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