Skip to content

Commit

Permalink
Merge pull request #804 from HairlessVillager/patch-1
Browse files Browse the repository at this point in the history
fix punctuation typo in ch10-02-traits.md
  • Loading branch information
KaiserY authored Jul 10, 2024
2 parents 94d7715 + 993adaa commit 743ff5e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ch10-02-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ fn some_function<T: Display + Clone, U: Clone + Debug>(t: &T, u: &U) -> i32 {
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-06-impl-trait-returns-one-type/src/lib.rs:here}}
```

这里尝试返回 `NewsArticle``Tweet`。这不能编译,因为 `impl Trait` 工作方式的限制。第十七章的 [顾及不同类型值的 trait 对象”][using-trait-objects-that-allow-for-values-of-different-types] 部分会介绍如何编写这样一个函数。
这里尝试返回 `NewsArticle``Tweet`。这不能编译,因为 `impl Trait` 工作方式的限制。第十七章的 [顾及不同类型值的 trait 对象”][using-trait-objects-that-allow-for-values-of-different-types] 部分会介绍如何编写这样一个函数。

### 使用 trait bound 有条件地实现方法

通过使用带有 trait bound 的泛型参数的 `impl` 块,可以有条件地只为那些实现了特定 trait 的类型实现方法。例如,示例 10-15 中的类型 `Pair<T>` 总是实现了 `new` 方法并返回一个 `Pair<T>` 的实例(回忆一下第五章的 ["定义方法"][methods] 部分,`Self` 是一个 `impl` 块类型的类型别名(type alias),在这里是 `Pair<T>`)。不过在下一个 `impl` 块中,只有那些为 `T` 类型实现了 `PartialOrd` trait(来允许比较) **** `Display` trait(来启用打印)的 `Pair<T>` 才会实现 `cmp_display` 方法:
通过使用带有 trait bound 的泛型参数的 `impl` 块,可以有条件地只为那些实现了特定 trait 的类型实现方法。例如,示例 10-15 中的类型 `Pair<T>` 总是实现了 `new` 方法并返回一个 `Pair<T>` 的实例(回忆一下第五章的 [定义方法][methods] 部分,`Self` 是一个 `impl` 块类型的类型别名(type alias),在这里是 `Pair<T>`)。不过在下一个 `impl` 块中,只有那些为 `T` 类型实现了 `PartialOrd` trait(来允许比较) **** `Display` trait(来启用打印)的 `Pair<T>` 才会实现 `cmp_display` 方法:

```rust,noplayground
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs}}
Expand Down

0 comments on commit 743ff5e

Please sign in to comment.