diff options
| author | Donough Liu <ldm2993593805@163.com> | 2019-12-30 14:41:46 +0800 |
|---|---|---|
| committer | Donough Liu <ldm2993593805@163.com> | 2019-12-30 14:41:46 +0800 |
| commit | 751fe7c43d428595d965d22ff21cc3c39dbccd5a (patch) | |
| tree | 10efc7d88186a751e89f90233bf50dcdfe699088 /src/librustc_error_codes/error_codes | |
| parent | 774a4bd4f4f6c91dda7c46550769349a522856f7 (diff) | |
| download | rust-751fe7c43d428595d965d22ff21cc3c39dbccd5a.tar.gz rust-751fe7c43d428595d965d22ff21cc3c39dbccd5a.zip | |
Add error code explanation for E0477
Diffstat (limited to 'src/librustc_error_codes/error_codes')
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0477.md | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/librustc_error_codes/error_codes/E0477.md b/src/librustc_error_codes/error_codes/E0477.md new file mode 100644 index 00000000000..29ce5343345 --- /dev/null +++ b/src/librustc_error_codes/error_codes/E0477.md @@ -0,0 +1,45 @@ +The type does not fulfill the required lifetime. + +Erroneous code example: + +```compile_fail,E0477 +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 doesn't satisfy the `'static` lifetime constraint. +To fix this kind of error, you need to double check 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 || { // error: this closure has lifetime 'a + // rather than 'static + println!("{}", s.lock().unwrap().data); + }); +} +``` |
