1
+ r[ lifetime-elision]
1
2
# Lifetime elision
2
3
3
4
Rust has rules that allow lifetimes to be elided in various places where the
4
5
compiler can infer a sensible default choice.
5
6
7
+ r[ lifetime-elision.function]
6
8
## Lifetime elision in functions
7
9
10
+ r[ lifetime-elision.function.intro]
8
11
In order to make common patterns more ergonomic, lifetime arguments can be
9
12
* elided* in [ function item] , [ function pointer] , and [ closure trait] signatures.
10
13
The 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
15
24
[ below] ( #default-trait-object-lifetimes ) .
16
25
26
+ r[ lifetime-elision.function.implicit-lifetime-parameters]
17
27
* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
28
+
29
+ r[ lifetime-elision.function.output-lifetime]
18
30
* If there is exactly one lifetime used in the parameters (elided or not), that
19
31
lifetime is assigned to * all* elided output lifetimes.
20
32
33
+ r[ lifetime-elision.function.receiver-lifetime]
21
34
In method signatures there is another rule
22
35
23
36
* 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
76
89
# }
77
90
```
78
91
92
+ r[ lifetime-elision.trait-object]
79
93
## Default trait object lifetimes
80
94
95
+ r[ lifetime-elision.trait-object.intro]
81
96
The assumed lifetime of references held by a [ trait object] is called its
82
97
_ default object lifetime bound_ . These were defined in [ RFC 599] and amended in
83
98
[ RFC 1156] .
84
99
100
+ r[ lifetime-elision.trait-object.explicit-bound]
85
101
These 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
88
106
rules.
89
107
108
+ r[ lifetime-elision.trait-object.containing-type]
90
109
If the trait object is used as a type argument of a generic type then the
91
110
containing type is first used to try to infer a bound.
92
111
112
+ r[ lifetime-elision.trait-object.containing-type-unique]
93
113
* 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]
94
116
* If there is more than one bound from the containing type then an explicit
95
117
bound must be specified
96
118
119
+ r[ lifetime-elision.trait-object.trait-bounds]
97
120
If neither of those rules apply, then the bounds on the trait are used:
98
121
122
+ r[ lifetime-elision.trait-object.trait-unique]
99
123
* If the trait is defined with a single lifetime _ bound_ then that bound is
100
124
used.
125
+
126
+ r[ lifetime-elision.trait-object.static-lifetime]
101
127
* If ` 'static ` is used for any lifetime bound then ` 'static ` is used.
128
+
129
+ r[ lifetime-elision.trait-object.default]
102
130
* If the trait has no lifetime bounds, then the lifetime is inferred in
103
131
expressions and is ` 'static ` outside of expressions.
104
132
@@ -136,6 +164,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
136
164
// Error: the lifetime bound for this object type cannot be deduced from context
137
165
```
138
166
167
+ r[ lifetime-elision.trait-object.innermost-type]
139
168
Note that the innermost object sets the bound, so ` &'a Box<dyn Foo> ` is still
140
169
` &'a Box<dyn Foo + 'static> ` .
141
170
@@ -152,8 +181,10 @@ impl<'a> dyn Bar<'a> {}
152
181
impl <'a > dyn Bar <'a > + 'a {}
153
182
```
154
183
155
- ## ` 'static ` lifetime elision
184
+ r[ lifetime-elision.const-static]
185
+ ## ` const ` and ` static ` elision
156
186
187
+ r[ lifetime-elision.const-static.implicit-static]
157
188
Both [ constant] and [ static] declarations of reference types have * implicit*
158
189
` 'static ` lifetimes unless an explicit lifetime is specified. As such, the
159
190
constant declarations involving ` 'static ` above may be written without the
@@ -175,6 +206,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
175
206
};
176
207
```
177
208
209
+ r[ lifetime-elision.const-static.fn-references]
178
210
Note that if the ` static ` or ` const ` items include function or closure
179
211
references, which themselves include references, the compiler will first try
180
212
the 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;
213
245
[ RFC 1156 ] : https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
214
246
[ static ] : items/static-items.md
215
247
[ 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