Rust 程序设计语言 简体中文版 1.85.0已经实现了名为 fly 方法的类型 Human 上实现这两个 trait。每一个 fly 方法都进行了不同的 操作: 文件名:src/main.rs trait Pilot { fn fly(&self); } trait Wizard { fn fly(&self); } struct Human; impl Pilot for Human { fn fly(&self) println!("This is your captain speaking."); } } impl Wizard for Human { fn fly(&self) { println!("Up!"); } } impl Human { fn fly(&self) { println!("*waving arms furiously*"); fly 方法,并在直接定义有 fly 方法的 Human 类型上实现 这两个 trait 当调用 Human 实例的 fly 时,编译器默认调用直接实现在该类型上的方法,如示例 20-18 所 示。 文件名:src/main.rs 480/562Rust 程序设计语言 简体中文版 fn main() { let person = Human; person.fly(); }0 码力 | 562 页 | 3.23 MB | 23 天前3
共 1 条
- 1













