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
@@ -75,27 +90,43 @@ fn frob(s: &str, t: &str) -> &str; // ILLEGAL
75
90
76
91
## Default trait object lifetimes
77
92
93
+ r[ lifetime-elision.trait-object]
94
+
95
+ r[ lifetime-elision.trait-object.intro]
78
96
The assumed lifetime of references held by a [ trait object] is called its
79
97
_ default object lifetime bound_ . These were defined in [ RFC 599] and amended in
80
98
[ RFC 1156] .
81
99
100
+ r[ lifetime-elision.trait-object.explicit-bound]
82
101
These 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
85
106
rules.
86
107
108
+ r[ lifetime-elision.trait-object.containing-type]
87
109
If the trait object is used as a type argument of a generic type then the
88
110
containing type is first used to try to infer a bound.
89
111
112
+ r[ lifetime-elision.trait-object.containing-type-unique]
90
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]
91
116
* If there is more than one bound from the containing type then an explicit
92
117
bound must be specified
93
118
119
+ r[ lifetime-elision.trait-object.trait-bounds]
94
120
If neither of those rules apply, then the bounds on the trait are used:
95
121
122
+ r[ lifetime-elision.trait-object.trait-unique]
96
123
* If the trait is defined with a single lifetime _ bound_ then that bound is
97
124
used.
125
+
126
+ r[ lifetime-elision.trait-object.static-lifetime]
98
127
* If ` 'static ` is used for any lifetime bound then ` 'static ` is used.
128
+
129
+ r[ lifetime-elision.trait-object.default]
99
130
* If the trait has no lifetime bounds, then the lifetime is inferred in
100
131
expressions and is ` 'static ` outside of expressions.
101
132
@@ -133,6 +164,7 @@ type T7<'a, 'b> = TwoBounds<'a, 'b, dyn Foo>;
133
164
// Error: the lifetime bound for this object type cannot be deduced from context
134
165
```
135
166
167
+ r[ lifetime-elision.trait-object.innermost-type]
136
168
Note that the innermost object sets the bound, so ` &'a Box<dyn Foo> ` is still
137
169
` &'a Box<dyn Foo + 'static> ` .
138
170
@@ -151,6 +183,9 @@ impl<'a> dyn Bar<'a> + 'a {}
151
183
152
184
## ` 'static ` lifetime elision
153
185
186
+ r[ lifetime-elision.item]
187
+
188
+ r[ lifetime-elision.item.intro]
154
189
Both [ constant] and [ static] declarations of reference types have * implicit*
155
190
` 'static ` lifetimes unless an explicit lifetime is specified. As such, the
156
191
constant declarations involving ` 'static ` above may be written without the
@@ -172,6 +207,7 @@ const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
172
207
};
173
208
```
174
209
210
+ r[ lifetime-elision.item.fn-types]
175
211
Note that if the ` static ` or ` const ` items include function or closure
176
212
references, which themselves include references, the compiler will first try
177
213
the standard elision rules. If it is unable to resolve the lifetimes by its
0 commit comments