特徵

Rust 可讓您依據特徵對型別進行抽象化處理,這與介面相似:

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.
  • 特徵用於定義型別必須具有哪幾個方法,才能實作該特徵。

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