blob: c6be8dc705e6e1435d7efad7454f4e41ce463be2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#### Note: this error code is no longer emitted by the compiler.
The type does not fulfill the required lifetime.
Erroneous code example:
```compile_fail
use std::sync::Mutex;
struct MyString<'a> {
data: &'a str,
}
fn i_want_static_closure<F>(a: F)
where F: Fn() + 'static {}
fn print_string<'a>(s: Mutex<MyString<'a>>) {
i_want_static_closure(move || { // error: this closure has lifetime 'a
// rather than 'static
println!("{}", s.lock().unwrap().data);
});
}
```
In this example, the closure does not satisfy the `'static` lifetime constraint.
To fix this error, you need to double check the lifetime of the type. Here, we
can fix this problem by giving `s` a static lifetime:
```
use std::sync::Mutex;
struct MyString<'a> {
data: &'a str,
}
fn i_want_static_closure<F>(a: F)
where F: Fn() + 'static {}
fn print_string(s: Mutex<MyString<'static>>) {
i_want_static_closure(move || { // ok!
println!("{}", s.lock().unwrap().data);
});
}
```
|