Citations

Finally we compare what we've covered about dyn Trait lifetime elision to the current reference material, and supply some citations to the elision's storied history.

Summary of differences from the reference

The official documentation on trait object lifetime elision can be found here.

In summary, it states that dyn Trait lifetimes have a default object lifetime bound which varies based on context. It states that the default bound only takes effect when the lifetime is entirely omitted. When you write out dyn Trait + '_, the normal lifetime elision rules apply instead.

In particular, as of this writing, the official documentation states that

If the trait object is used as a type argument of a generic type then the containing type is first used to try to infer a bound.

  • If there is a unique bound from the containing type then that is the default
  • If there is more than one bound from the containing type then an explicit bound must be specified

If neither of those rules apply, then the bounds on the trait are used:

  • If the trait is defined with a single lifetime bound then that bound is used.
  • If 'static is used for any lifetime bound then 'static is used.
  • If the trait has no lifetime bounds, then the lifetime is inferred in expressions and is 'static outside of expressions.

Some differences from the reference which we have covered are that

And some other under or undocumented behaviors are that

RFCs, Issues, and PRs

Trait objects, and trait object lifetime elision in particular, has undergone a lot of evolution over time. Here we summarize some of the major developments and issues.

Reminder: a lot of these citations predate the dyn Trait syntax. Trait objects used to be just "spelled" as Trait in type position, instead of dyn Trait.