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).
5 Erroneous code examples:
8 struct Foo1 { x: &bool }
9 // ^ expected lifetime parameter
10 struct Foo2<'a> { x: &'a bool } // correct
12 struct Bar1 { x: Foo2 }
13 // ^^^^ expected lifetime parameter
14 struct Bar2<'a> { x: Foo2<'a> } // correct
16 enum Baz1 { A(u8), B(&bool), }
17 // ^ expected lifetime parameter
18 enum Baz2<'a> { A(u8), B(&'a bool), } // correct
21 // ^ expected lifetime parameter
22 type MyStr2<'a> = &'a str; // correct
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].
29 The lifetime elision rules require that any function signature with an elided
30 output lifetime must either have:
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
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`.
40 Here are some examples of elision errors:
43 // error, no input lifetimes
46 // error, `x` and `y` have distinct lifetimes inferred
47 fn bar(x: &str, y: &str) -> &str { }
49 // error, `y`'s lifetime is inferred to be distinct from `x`'s
50 fn baz<'a>(x: &'a str, y: &str) -> &str { }
53 [book-le]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision