mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2025-03-14 03:10:44 +08:00
Update Ch10
This commit is contained in:
parent
375b544034
commit
70d27c832f
@ -801,7 +801,7 @@ fn returns_summarizable(switch: bool) -> impl Summary {
|
||||
|
||||
### 运用特质边界来有条件地实现方法
|
||||
|
||||
经由使用带有用到了泛型参数 `impl` 代码块的特质边界,咱们可有条件地对实现了指定特质的类型实现方法,by using a trait bound with an `impl` block that uses generic type parameters, we can implement methods conditionally for types that implement the specified traits。比如下面清单 10-15 中的类型 `Pair<T>`,就会一直将那个 `new` 函数,实现为返回 `Pair<T>` 的新实例(回顾第 5 章的 [定义方法](Ch05_Using_Structs_to_Structure_Related_Data.md#方法的定义) 小节就知道,`Self` 就是那个 `impl` 代码块的类型别名,此示例中即 `Pair<T>`)。但在接下来的 `impl` 代码块中,若 `Pair` 只在其内部类型 `T` 内,实现启用比较的 `PartialOrd` 特质,*与* 启用打印的 `Display` 特质,那么其就只会实现 `cmp_display` 方法。
|
||||
使用带有用到泛型参数 `impl` 代码块的特质边界,咱们便可根据实现了指定特质的类型,而有条件地实现方法,by using a trait bound with an `impl` block that uses generic type parameters, we can implement methods conditionally for types that implement the specified traits。比如下面清单 10-15 中的类型 `Pair<T>`,就会一直将那个 `new` 函数,实现为返回 `Pair<T>` 的新实例(回顾第 5 章的 [定义方法](Ch05_Using_Structs_to_Structure_Related_Data.md#方法的定义) 小节就知道,`Self` 就是那个 `impl` 代码块的类型别名,此示例中即 `Pair<T>`)。但在接下来的 `impl` 代码块中,若 `Pair<T>` 只在其内部类型 `T` 里,实现启用比较的 `PartialOrd` 特质,*与* 启用打印的 `Display` 特质,那么 `Pair<T>` 就只会实现 `cmp_display` 方法。
|
||||
|
||||
|
||||
```rust
|
||||
|
Loading…
Reference in New Issue
Block a user