Technology Sharing

Rust Study Notes 007: Trait --- Rust's "Interface"

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Trait

  • In Rust, a trait is a mechanism for defining a set of methods, similar to an interface (Java) or an abstract class (Virtual functions of c)。

Defining Traits

trait MyTrait { //关键字trait(线条、特征、勾勒)
    // 定义Trait的方法
    fn my_method(