]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0106.md
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0106.md
1 This error indicates that a lifetime is missing from a type. If it is an error
2 inside a function signature, the problem may be with failing to adhere to the
3 lifetime elision rules (see below).
4
5 Erroneous code examples:
6
7 ```compile_fail,E0106
8 struct Foo1 { x: &bool }
9 // ^ expected lifetime parameter
10 struct Foo2<'a> { x: &'a bool } // correct
11
12 struct Bar1 { x: Foo2 }
13 // ^^^^ expected lifetime parameter
14 struct Bar2<'a> { x: Foo2<'a> } // correct
15
16 enum Baz1 { A(u8), B(&bool), }
17 // ^ expected lifetime parameter
18 enum Baz2<'a> { A(u8), B(&'a bool), } // correct
19
20 type MyStr1 = &str;
21 // ^ expected lifetime parameter
22 type MyStr2<'a> = &'a str; // correct
23 ```
24
25 Lifetime elision is a special, limited kind of inference for lifetimes in
26 function signatures which allows you to leave out lifetimes in certain cases.
27 For more background on lifetime elision see [the book][book-le].
28
29 The lifetime elision rules require that any function signature with an elided
30 output lifetime must either have:
31
32 - exactly one input lifetime
33 - or, multiple input lifetimes, but the function must also be a method with a
34 `&self` or `&mut self` receiver
35
36 In the first case, the output lifetime is inferred to be the same as the unique
37 input lifetime. In the second case, the lifetime is instead inferred to be the
38 same as the lifetime on `&self` or `&mut self`.
39
40 Here are some examples of elision errors:
41
42 ```compile_fail,E0106
43 // error, no input lifetimes
44 fn foo() -> &str { }
45
46 // error, `x` and `y` have distinct lifetimes inferred
47 fn bar(x: &str, y: &str) -> &str { }
48
49 // error, `y`'s lifetime is inferred to be distinct from `x`'s
50 fn baz<'a>(x: &'a str, y: &str) -> &str { }
51 ```
52
53 [book-le]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision