|
|
@ -98,7 +98,7 @@ trait Add<Rhs=Self> {
|
|
|
|
|
|
|
|
|
|
|
|
### 在同名方法之间消歧义
|
|
|
|
### 在同名方法之间消歧义
|
|
|
|
|
|
|
|
|
|
|
|
Rust 既不能避免一个 trait 与另一个 trait 拥有相同名称的方法,也不能阻止为同一类型同时实现这两个 trait。同时也可以直接在类型上实现一个与 trait 方法同名的方法。
|
|
|
|
Rust 既不能避免一个 trait 与另一个 trait 拥有相同名称的方法,也不能阻止为同一类型同时实现这两个 trait。同时还可以直接在类型上实现一个与 trait 方法同名的方法。
|
|
|
|
|
|
|
|
|
|
|
|
当调用这些同名方法时,需要告诉 Rust 我们想要使用哪一个。考虑一下示例 20-17 中的代码,这里我们定义了两个 trait,`Pilot` 和 `Wizard`,它们都拥有名为 `fly` 的方法。接着在一个本身已经实现了名为 `fly` 方法的类型 `Human` 上实现这两个 trait。每一个 `fly` 方法都进行了不同的操作:
|
|
|
|
当调用这些同名方法时,需要告诉 Rust 我们想要使用哪一个。考虑一下示例 20-17 中的代码,这里我们定义了两个 trait,`Pilot` 和 `Wizard`,它们都拥有名为 `fly` 的方法。接着在一个本身已经实现了名为 `fly` 方法的类型 `Human` 上实现这两个 trait。每一个 `fly` 方法都进行了不同的操作:
|
|
|
|
|
|
|
|
|
|
|
|