]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0581.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0581.md
1 In a `fn` type, a lifetime appears only in the return type
2 and not in the arguments types.
3
4 Erroneous code example:
5
6 ```compile_fail,E0581
7 fn main() {
8     // Here, `'a` appears only in the return type:
9     let x: for<'a> fn() -> &'a i32;
10 }
11 ```
12
13 The problem here is that the lifetime isn't constrained by any of the arguments,
14 making it impossible to determine how long it's supposed to live.
15
16 To fix this issue, either use the lifetime in the arguments, or use the
17 `'static` lifetime. Example:
18
19 ```
20 fn main() {
21     // Here, `'a` appears only in the return type:
22     let x: for<'a> fn(&'a i32) -> &'a i32;
23     let y: fn() -> &'static i32;
24 }
25 ```
26
27 Note: The examples above used to be (erroneously) accepted by the
28 compiler, but this was since corrected. See [issue #33685] for more
29 details.
30
31 [issue #33685]: https://github.com/rust-lang/rust/issues/33685