Traits

Rust lets you abstract over types with traits. They’re similar to interfaces:

  1. trait Pet {
  2.     /// Return a sentence from this pet.
  3.     fn talk(&self) -> String;
  4.     /// Print a string to the terminal greeting this pet.
  5.     fn greet(&self);
  6. }

This slide and its sub-slides should take about 15 minutes.

  • A trait defines a number of methods that types must have in order to implement the trait.

  • In the “Generics” segment, next, we will see how to build functionality that is generic over all types implementing a trait.