05-使用 Trait 实现多态

使用 Trait 实现多态 — Rust 语言之旅

内容来源 · Tour of Rust / Rust 语言之旅

原文链接: https://tourofrust.com/78_zh-cn.html

使用 Trait 实现多态

Rust 支持多态的特性。Trait 允许我们将一组方法与结构类型关联起来。

我们首先在 Trait 里面定义函数签名:

trait MyTrait {
    fn foo(&self);
    ...
}

当一个结构体实现一个 trait 时,它便建立了一个契约,允许我们通过 trait 类型与结构体进行间接交互(例如 &dyn MyTrait),而不必知道其真实的类型。

结构体实现 Trait 方法是在实现块中定义要实现的方法:

1
2
3
4
5
6
impl MyTrait for MyStruct { 
    fn foo(&self) {
        ...
    }
    ... 
}

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct SeaCreature {
    pub name: String,
    noise: String,
}

impl SeaCreature {
    pub fn get_sound(&self) -> &str {
        &self.noise
    }
}

trait NoiseMaker {
    fn make_noise(&self);
}

impl NoiseMaker for SeaCreature {
    fn make_noise(&self) {
        println!("{}", &self.get_sound());
    }
}

fn main() {
    let creature = SeaCreature {
        name: String::from("Ferris"),
        noise: String::from("blub"),
    };
    creature.make_noise();
}
最后修改 August 21, 2026: 更新 (76fc81a2e)