Traits

Rust permite abstrair caracterĂ­sticas dos tipos usando trait. Eles sĂŁo semelhantes a interfaces:

trait Pet {
    /// Return a sentence from this pet.
    fn talk(&self) -> String;

    /// Print a string to the terminal greeting this pet.
    fn greet(&self);
}
This slide and its sub-slides should take about 10 minutes.
  • Um trait define um nĂșmero de mĂ©todos que os tipos devem ter para implementar o trait.

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