You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Trait] and lifetime bounds provide a way for [generic items][generic] to
39
43
restrict which types and lifetimes are used as their parameters. Bounds can be
40
44
provided on any type in a [where clause]. There are also shorter forms for
@@ -48,6 +52,7 @@ certain common cases:
48
52
`trait A { type B: Copy; }` is equivalent to
49
53
`trait A where Self::B: Copy { type B; }`.
50
54
55
+
r[bound.satisfaction]
51
56
Bounds on an item must be satisfied when using the item. When type checking and
52
57
borrow checking a generic item, the bounds can be used to determine that a
53
58
trait is implemented for a type. For example, given `Ty: Trait`
@@ -87,9 +92,11 @@ fn name_figure<U: Shape>(
87
92
}
88
93
```
89
94
95
+
r[bound.trivial]
90
96
Bounds that don't use the item's parameters or [higher-ranked lifetimes] are checked when the item is defined.
91
97
It is an error for such a bound to be false.
92
98
99
+
r[bound.special]
93
100
[`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic types when using the item, even if the use does not provide a concrete type.
94
101
It is an error to have `Copy` or `Clone` as a bound on a mutable reference, [trait object], or [slice].
95
102
It is an error to have `Sized` as a bound on a trait object or slice.
@@ -107,16 +114,24 @@ where
107
114
struct UsesA<'a, T>(A<'a, T>);
108
115
```
109
116
117
+
r[bound.trait-object]
110
118
Trait and lifetime bounds are also used to name [trait objects].
111
119
112
120
## `?Sized`
113
121
122
+
r[bound.sized]
123
+
114
124
`?` is only used to relax the implicit [`Sized`] trait bound for [type parameters] or [associated types].
115
125
`?Sized` may not be used as a bound for other types.
116
126
117
127
## Lifetime bounds
118
128
129
+
r[bound.lifetime]
130
+
131
+
r[bound.lifetime.intro]
119
132
Lifetime bounds can be applied to types or to other lifetimes.
133
+
134
+
r[bound.lifetime.outlive-lifetime]
120
135
The bound `'a: 'b` is usually read as `'a`*outlives*`'b`.
121
136
`'a: 'b` means that `'a` lasts at least as long as `'b`, so a reference `&'a ()` is valid whenever `&'b ()` is valid.
`T: 'a` means that all lifetime parameters of `T` outlive `'a`.
131
147
For example, if `'a` is an unconstrained lifetime parameter, then `i32: 'static` and `&'static str: 'a` are satisfied, but `Vec<&'a ()>: 'static` is not.
132
148
133
149
## Higher-ranked trait bounds
134
150
151
+
r[bound.higher-ranked]
152
+
153
+
r[bound.higher-ranked.syntax]
135
154
> _ForLifetimes_ :\
136
155
> `for`[_GenericParams_]
137
156
157
+
r[bound.higher-ranked.intro]
138
158
Trait bounds may be *higher ranked* over lifetimes. These bounds specify a bound
139
159
that is true *for all* lifetimes. For example, a bound such as `for<'a> &'a T:
140
160
PartialEq<i32>` would require an implementation like
Lifetime bounds required for types to be well-formed are sometimes inferred.
176
200
177
201
```rust
178
202
fnrequires_t_outlives_a<'a, T>(x:&'aT) {}
179
203
```
204
+
180
205
The type parameter `T` is required to outlive `'a` for the type `&'a T` to be well-formed.
181
206
This is inferred because the function signature contains the type `&'a T` which is
182
207
only valid if `T: 'a` holds.
183
208
209
+
r[bound.implied.context]
184
210
Implied bounds are added for all parameters and outputs of functions. Inside of `requires_t_outlives_a`
185
211
you can assume `T: 'a` to hold even if you don't explicitly specify this:
186
212
@@ -203,6 +229,7 @@ fn not_implied<'a, T>() {
203
229
}
204
230
```
205
231
232
+
r[bound.implied.trait]
206
233
Only lifetime bounds are implied, trait bounds still have to be explicitly added.
207
234
The following example therefore causes an error:
208
235
@@ -213,6 +240,7 @@ struct IsDebug<T: Debug>(T);
213
240
fn doesnt_specify_t_debug<T>(x: IsDebug<T>) {}
214
241
```
215
242
243
+
r[bound.implied.def]
216
244
Lifetime bounds are also inferred for type definitions and impl blocks for any type:
217
245
218
246
```rust
@@ -244,6 +272,8 @@ impl<'a, T> Trait<'a, T> for &'a T {}
244
272
245
273
## Use bounds
246
274
275
+
r[bound.use]
276
+
247
277
Certain bounds lists may include a `use<..>` bound to control which generic parameters are captured by the `impl Trait`[abstract return type]. See [precise capturing] for more details.
0 commit comments