ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Rust】特质-trait

2021-11-30 23:03:00  阅读:36  来源: 互联网

标签:特质 name trait self Person say fn hello Rust


环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

参考:https://doc.rust-lang.org/stable/rust-by-example/trait.html

先简单地认为 trait 就是其它语言中的接口,可以为不同的类型定义同一种行为。

示例

Person

struct Person {
    name: String,
}

trait Say {
    fn say_hello(&self);
}

impl Say for Person {
    fn say_hello(&self) {
        println!("{} say hello", self.name);
    }
}

fn main() {
    let person = Person {
        name: "jiangbo".to_string(),
    };

    person.say_hello();
}

Dog

struct Person {
    name: String,
}

struct Dog {
    name: String,
}

trait Say {
    fn say_hello(&self);
}

impl Say for Person {
    fn say_hello(&self) {
        println!("{} say hello", self.name);
    }
}

impl Say for Dog {
    fn say_hello(&self) {
        println!("{} say wang wang", self.name);
    }
}

fn main() {
    let person = Person {
        name: "jiangbo".to_string(),
    };

    let dog = Dog {
        name: "wangcai".to_string(),
    };

    person.say_hello();
    dog.say_hello();
}

总结

了解了 Rust 中 trait 一般翻译为特质,或者直接叫英文,和其它语言中的接口类似。

附录

标签:特质,name,trait,self,Person,say,fn,hello,Rust
来源: https://www.cnblogs.com/jiangbo44/p/15626837.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有