Skip to content

Commit b113f58

Browse files
committed
Explain default trait object liftime bounds.
Fixes rust-lang#31137
1 parent 91e8044 commit b113f58

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/doc/reference.md

+35
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,41 @@ fn main() {
36033603
In this example, the trait `Printable` occurs as a trait object in both the
36043604
type signature of `print`, and the cast expression in `main`.
36053605

3606+
Trait objects may contain references, and so those references will need a
3607+
lifetime. If the trait contains some sort of lifetime bound, like this:
3608+
3609+
```rust,ignore
3610+
// Some trait like this...
3611+
trait Bar<'a>: 'a { }
3612+
3613+
// ... means these are the same:
3614+
Box<Bar>
3615+
Box<Bar + 'a>
3616+
```
3617+
3618+
If there’s no bound, then the default lifetime is the same as the pointer that
3619+
the trait object is behind:
3620+
3621+
```rust,ignore
3622+
// Some trait like this...
3623+
trait Bar { }
3624+
3625+
// ... means these are the same:
3626+
&Bar
3627+
&'a (Bar + 'a)
3628+
```
3629+
3630+
In any other case, the default is `'static`:
3631+
3632+
```rust,ignore
3633+
// Some trait like this...
3634+
trait Bar { }
3635+
3636+
// ... means these are the same:
3637+
Box<Bar>
3638+
Box<Bar + 'static>
3639+
```
3640+
36063641
### Type parameters
36073642

36083643
Within the body of an item that has type parameter declarations, the names of

0 commit comments

Comments
 (0)