Skip to content

Commit

Permalink
Explain default trait object liftime bounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Feb 2, 2016
1 parent 91e8044 commit b113f58
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3603,6 +3603,41 @@ fn main() {
In this example, the trait `Printable` occurs as a trait object in both the
type signature of `print`, and the cast expression in `main`.

Trait objects may contain references, and so those references will need a
lifetime. If the trait contains some sort of lifetime bound, like this:

```rust,ignore
// Some trait like this...
trait Bar<'a>: 'a { }
// ... means these are the same:
Box<Bar>
Box<Bar + 'a>
```

If there’s no bound, then the default lifetime is the same as the pointer that
the trait object is behind:

```rust,ignore
// Some trait like this...
trait Bar { }
// ... means these are the same:
&Bar
&'a (Bar + 'a)
```

In any other case, the default is `'static`:

```rust,ignore
// Some trait like this...
trait Bar { }
// ... means these are the same:
Box<Bar>
Box<Bar + 'static>
```

### Type parameters

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

0 comments on commit b113f58

Please sign in to comment.