11# Lifetime elision
22
3+ r[ lifetime-elision]
4+
35Rust has rules that allow lifetimes to be elided in various places where the
46compiler can infer a sensible default choice.
57
68## Lifetime elision in functions
79
10+ r[ lifetime-elision.function]
11+
12+ r[ lifetime-elision.function.intro]
813In order to make common patterns more ergonomic, lifetime arguments can be
914* elided* in [ function item] , [ function pointer] , and [ closure trait] signatures.
1015The following rules are used to infer lifetime parameters for elided lifetimes.
11- It is an error to elide lifetime parameters that cannot be inferred. The
12- placeholder lifetime, ` '_ ` , can also be used to have a lifetime inferred in the
13- same way. For lifetimes in paths, using ` '_ ` is preferred. Trait object
14- lifetimes follow different rules discussed
16+
17+ r[ lifetime-elision.function.constraint]
18+ It is an error to elide lifetime parameters that cannot be inferred.
19+
20+ r[ lifetime-elision.function.explicit-placeholder]
21+ The placeholder lifetime, ` '_ ` , can also be used to have a lifetime inferred in the
22+ same way. For lifetimes in paths, using ` '_ ` is preferred.
23+
24+ r[ lifetime-elision.function.only-functions]
25+ Trait object lifetimes follow different rules discussed
1526[ below] ( #default-trait-object-lifetimes ) .
1627
28+ r[ lifetime-elision.function.implicit-lifetime-parameters]
1729* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
30+
31+ r[ lifetime-elision.function.output-lifetime]
1832* If there is exactly one lifetime used in the parameters (elided or not), that
1933 lifetime is assigned to * all* elided output lifetimes.
2034
35+ r[ lifetime-elision.function.reciever-lifetime]
2136In method signatures there is another rule
2237
2338* If the receiver has type ` &Self ` or ` &mut Self ` , then the lifetime of that
@@ -78,27 +93,43 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
7893
7994## Default trait object lifetimes
8095
96+ r[ lifetime-elision.trait-object]
97+
98+ r[ lifetime-elision.trait-object.intro]
8199The assumed lifetime of references held by a [ trait object] is called its
82100_ default object lifetime bound_ . These were defined in [ RFC 599] and amended in
83101[ RFC 1156] .
84102
103+ r[ lifetime-elision.trait-object.explicit-bound]
85104These default object lifetime bounds are used instead of the lifetime parameter
86- elision rules defined above when the lifetime bound is omitted entirely. If
87- ` '_ ` is used as the lifetime bound then the bound follows the usual elision
105+ elision rules defined above when the lifetime bound is omitted entirely.
106+
107+ r[ lifetime-elision.trait-object.explicit-placeholder]
108+ If ` '_ ` is used as the lifetime bound then the bound follows the usual elision
88109rules.
89110
111+ r[ lifetime-elision.trait-object.containing-type]
90112If the trait object is used as a type argument of a generic type then the
91113containing type is first used to try to infer a bound.
92114
115+ r[ lifetime-elision.trait-object.containing-type-unique]
93116* If there is a unique bound from the containing type then that is the default
117+
118+ r[ lifetime-elision.trait-object.containing-type-explicit]
94119* If there is more than one bound from the containing type then an explicit
95120 bound must be specified
96121
122+ r[ lifetime-elision.trait-object.trait-bounds]
97123If neither of those rules apply, then the bounds on the trait are used:
98124
125+ r[ lifetime-elision.trait-object.trait-unique]
99126* If the trait is defined with a single lifetime _ bound_ then that bound is
100127 used.
128+
129+ r[ lifetime-elision.trait-object.static-lifetime]
101130* If ` 'static ` is used for any lifetime bound then ` 'static ` is used.
131+
132+ r[ lifetime-elision.trait-object.default]
102133* If the trait has no lifetime bounds, then the lifetime is inferred in
103134 expressions and is ` 'static ` outside of expressions.
104135
@@ -136,6 +167,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
136167// Error: the lifetime bound for this object type cannot be deduced from context
137168```
138169
170+ r[ lifetime-elision.trait-object.innermost-type]
139171Note that the innermost object sets the bound, so ` &'a Box<dyn Foo> ` is still
140172` &'a Box<dyn Foo + 'static> ` .
141173
@@ -154,6 +186,9 @@ impl<'a> dyn Bar<'a> + 'a {}
154186
155187## ` 'static ` lifetime elision
156188
189+ r[ lifetime-elision.item]
190+
191+ r[ lifetime-elision.item.intro]
157192Both [ constant] and [ static] declarations of reference types have * implicit*
158193` 'static ` lifetimes unless an explicit lifetime is specified. As such, the
159194constant declarations involving ` 'static ` above may be written without the
@@ -175,6 +210,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
175210};
176211```
177212
213+ r[ lifetime-elision.item.fn-types]
178214Note that if the ` static ` or ` const ` items include function or closure
179215references, which themselves include references, the compiler will first try
180216the standard elision rules. If it is unable to resolve the lifetimes by its
0 commit comments