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