1+ r[ lifetime-elision]
12# Lifetime elision
23
34Rust has rules that allow lifetimes to be elided in various places where the
45compiler can infer a sensible default choice.
56
7+ r[ lifetime-elision.function]
68## Lifetime elision in functions
79
10+ r[ lifetime-elision.function.intro]
811In order to make common patterns more ergonomic, lifetime arguments can be
912* elided* in [ function item] , [ function pointer] , and [ closure trait] signatures.
1013The 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
14+
15+ r[ lifetime-elision.function.lifetimes-not-inferred]
16+ It is an error to elide lifetime parameters that cannot be inferred.
17+
18+ r[ lifetime-elision.function.explicit-placeholder]
19+ The placeholder lifetime, ` '_ ` , can also be used to have a lifetime inferred in the
20+ same way. For lifetimes in paths, using ` '_ ` is preferred.
21+
22+ r[ lifetime-elision.function.only-functions]
23+ Trait object lifetimes follow different rules discussed
1524[ below] ( #default-trait-object-lifetimes ) .
1625
26+ r[ lifetime-elision.function.implicit-lifetime-parameters]
1727* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
28+
29+ r[ lifetime-elision.function.output-lifetime]
1830* If there is exactly one lifetime used in the parameters (elided or not), that
1931 lifetime is assigned to * all* elided output lifetimes.
2032
33+ r[ lifetime-elision.function.receiver-lifetime]
2134In method signatures there is another rule
2235
2336* If the receiver has type ` &Self ` or ` &mut Self ` , then the lifetime of that
@@ -76,29 +89,44 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
7689# }
7790```
7891
92+ r[ lifetime-elision.trait-object]
7993## Default trait object lifetimes
8094
95+ r[ lifetime-elision.trait-object.intro]
8196The assumed lifetime of references held by a [ trait object] is called its
8297_ default object lifetime bound_ . These were defined in [ RFC 599] and amended in
8398[ RFC 1156] .
8499
100+ r[ lifetime-elision.trait-object.explicit-bound]
85101These 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
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
88106rules.
89107
108+ r[ lifetime-elision.trait-object.containing-type]
90109If the trait object is used as a type argument of a generic type then the
91110containing type is first used to try to infer a bound.
92111
112+ r[ lifetime-elision.trait-object.containing-type-unique]
93113* 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]
94116* If there is more than one bound from the containing type then an explicit
95117 bound must be specified
96118
119+ r[ lifetime-elision.trait-object.trait-bounds]
97120If neither of those rules apply, then the bounds on the trait are used:
98121
122+ r[ lifetime-elision.trait-object.trait-unique]
99123* If the trait is defined with a single lifetime _ bound_ then that bound is
100124 used.
125+
126+ r[ lifetime-elision.trait-object.static-lifetime]
101127* If ` 'static ` is used for any lifetime bound then ` 'static ` is used.
128+
129+ r[ lifetime-elision.trait-object.default]
102130* If the trait has no lifetime bounds, then the lifetime is inferred in
103131 expressions and is ` 'static ` outside of expressions.
104132
@@ -136,6 +164,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
136164// Error: the lifetime bound for this object type cannot be deduced from context
137165```
138166
167+ r[ lifetime-elision.trait-object.innermost-type]
139168Note that the innermost object sets the bound, so ` &'a Box<dyn Foo> ` is still
140169` &'a Box<dyn Foo + 'static> ` .
141170
@@ -152,8 +181,10 @@ impl<'a> dyn Bar<'a> {}
152181impl <'a > dyn Bar <'a > + 'a {}
153182```
154183
155- ## ` 'static ` lifetime elision
184+ r[ lifetime-elision.const-static]
185+ ## ` const ` and ` static ` elision
156186
187+ r[ lifetime-elision.const-static.implicit-static]
157188Both [ constant] and [ static] declarations of reference types have * implicit*
158189` 'static ` lifetimes unless an explicit lifetime is specified. As such, the
159190constant declarations involving ` 'static ` above may be written without the
@@ -175,6 +206,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
175206};
176207```
177208
209+ r[ lifetime-elision.const-static.fn-references]
178210Note that if the ` static ` or ` const ` items include function or closure
179211references, which themselves include references, the compiler will first try
180212the standard elision rules. If it is unable to resolve the lifetimes by its
@@ -213,3 +245,17 @@ const RESOLVED_STATIC: &dyn Fn(&Foo, &Bar) -> &Baz = &somefunc;
213245[ RFC 1156 ] : https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
214246[ static ] : items/static-items.md
215247[ trait object ] : types/trait-object.md
248+
249+ <script >
250+ (function () {
251+ var fragments = {
252+ " #static-lifetime-elision" : " lifetime-elision.html#const-and-static-elision" ,
253+ };
254+ var target = fragments[window .location .hash ];
255+ if (target) {
256+ var url = window .location .toString ();
257+ var base = url .substring (0 , url .lastIndexOf (' /' ));
258+ window .location .replace (base + " /" + target);
259+ }
260+ })();
261+ </script >
0 commit comments