]> git.proxmox.com Git - rustc.git/blame - src/librustc_error_codes/error_codes/E0477.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0477.md
CommitLineData
dfeec247
XL
1The type does not fulfill the required lifetime.
2
3Erroneous code example:
4
5```compile_fail,E0477
6use std::sync::Mutex;
7
8struct MyString<'a> {
9 data: &'a str,
10}
11
12fn i_want_static_closure<F>(a: F)
13 where F: Fn() + 'static {}
14
15fn print_string<'a>(s: Mutex<MyString<'a>>) {
16
17 i_want_static_closure(move || { // error: this closure has lifetime 'a
18 // rather than 'static
19 println!("{}", s.lock().unwrap().data);
20 });
21}
22```
23
24In this example, the closure does not satisfy the `'static` lifetime constraint.
25To fix this error, you need to double check the lifetime of the type. Here, we
26can fix this problem by giving `s` a static lifetime:
27
28```
29use std::sync::Mutex;
30
31struct MyString<'a> {
32 data: &'a str,
33}
34
35fn i_want_static_closure<F>(a: F)
36 where F: Fn() + 'static {}
37
38fn print_string(s: Mutex<MyString<'static>>) {
39
3dfed10e 40 i_want_static_closure(move || { // ok!
dfeec247
XL
41 println!("{}", s.lock().unwrap().data);
42 });
43}
44```