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
@@ -75,27 +90,43 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
7590
7691## Default trait object lifetimes
7792
93+ r[ lifetime-elision.trait-object]
94+
95+ r[ lifetime-elision.trait-object.intro]
7896The assumed lifetime of references held by a [ trait object] is called its
7997_ default object lifetime bound_ . These were defined in [ RFC 599] and amended in
8098[ RFC 1156] .
8199
100+ r[ lifetime-elision.trait-object.explicit-bound]
82101These default object lifetime bounds are used instead of the lifetime parameter
83- elision rules defined above when the lifetime bound is omitted entirely. If
84- ` '_ ` is used as the lifetime bound then the bound follows the usual elision
102+ elision rules defined above when the lifetime bound is omitted entirely.
103+
104+ r[ lifetime-elision.trait-object.explicit-placeholder]
105+ If ` '_ ` is used as the lifetime bound then the bound follows the usual elision
85106rules.
86107
108+ r[ lifetime-elision.trait-object.containing-type]
87109If the trait object is used as a type argument of a generic type then the
88110containing type is first used to try to infer a bound.
89111
112+ r[ lifetime-elision.trait-object.containing-type-unique]
90113* If there is a unique bound from the containing type then that is the default
114+
115+ r[ lifetime-elision.trait-object.containing-type-explicit]
91116* If there is more than one bound from the containing type then an explicit
92117 bound must be specified
93118
119+ r[ lifetime-elision.trait-object.trait-bounds]
94120If neither of those rules apply, then the bounds on the trait are used:
95121
122+ r[ lifetime-elision.trait-object.trait-unique]
96123* If the trait is defined with a single lifetime _ bound_ then that bound is
97124 used.
125+
126+ r[ lifetime-elision.trait-object.static-lifetime]
98127* If ` 'static ` is used for any lifetime bound then ` 'static ` is used.
128+
129+ r[ lifetime-elision.trait-object.default]
99130* If the trait has no lifetime bounds, then the lifetime is inferred in
100131 expressions and is ` 'static ` outside of expressions.
101132
@@ -133,6 +164,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
133164// Error: the lifetime bound for this object type cannot be deduced from context
134165```
135166
167+ r[ lifetime-elision.trait-object.innermost-type]
136168Note that the innermost object sets the bound, so ` &'a Box<dyn Foo> ` is still
137169` &'a Box<dyn Foo + 'static> ` .
138170
@@ -151,6 +183,9 @@ impl<'a> dyn Bar<'a> + 'a {}
151183
152184## ` 'static ` lifetime elision
153185
186+ r[ lifetime-elision.item]
187+
188+ r[ lifetime-elision.item.intro]
154189Both [ constant] and [ static] declarations of reference types have * implicit*
155190` 'static ` lifetimes unless an explicit lifetime is specified. As such, the
156191constant declarations involving ` 'static ` above may be written without the
@@ -172,6 +207,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
172207};
173208```
174209
210+ r[ lifetime-elision.item.fn-types]
175211Note that if the ` static ` or ` const ` items include function or closure
176212references, which themselves include references, the compiler will first try
177213the standard elision rules. If it is unable to resolve the lifetimes by its
0 commit comments