New keywords

Minimum Rust version: 1.27

Summary

Motivation

dyn Trait for trait objects

The dyn Trait feature is the new syntax for using trait objects. In short:

  • Box<Trait> becomes Box<dyn Trait>
  • &Trait and &mut Trait become &dyn Trait and &mut dyn Trait

And so on. In code:


#![allow(unused)]
fn main() {
trait Trait {}

impl Trait for i32 {}

// old
fn function1() -> Box<Trait> {
unimplemented!()
}

// new
fn function2() -> Box<dyn Trait> {
unimplemented!()
}
}

That's it!

Why?

Using just the trait name for trait objects turned out to be a bad decision. The current syntax is often ambiguous and confusing, even to veterans, and favors a feature that is not more frequently used than its alternatives, is sometimes slower, and often cannot be used at all when its alternatives can.

Furthermore, with impl Trait arriving, "impl Trait vs dyn Trait" is much more symmetric, and therefore a bit nicer, than "impl Trait vs Trait". impl Trait is explained here.

In the new edition, you should therefore prefer dyn Trait to just Trait where you need a trait object.

async and await

These keywords are reserved to implement the async-await feature of Rust, which was ultimately released to stable in 1.39.0.

try keyword

The try keyword is reserved for use in try blocks, which have not (as of this writing) been stabilized (tracking issue)