知识卡片
泛型约束声明所需能力
泛型函数若调用T的排序行为,就要用trait bound声明该能力,编译器才能在实现与调用处检查。dump_sorted要求T:Sort+IntoIterator,还需T::Item:Debug才能打印元素。集合能迭代并不意味着元素能格式化;约束应落在真正使用该能力的类型上。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Trait bounds · 查看原书依据
Trait bounds A trait bound indicates that generic code that is parameterized by some type T can be used only when that type T implements some specific trait. The presence of the trait bound means that the implementation of the generic can use the functions from that trait, secure in the knowledge that the compiler will ensure that any T that compiles does in
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Trait bounds》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u020-ch01-020)。
- 结论依据:原文在“Trait bounds”附近的定义、例子或代码支撑本卡核心说明:泛型函数若调用T的排序行为,就要用trait bound声明该能力,编译器才能在实现与调用处检查。边界按已读前提解释;示例未执行。
- 原始内容:Trait bounds A trait bound indicates that generic code that is parameterized by some type T can be used only when that type T implements some specific trait. The presence of the trait bound means that the implementation of the generic can use the functions from that trait, secure in the knowledge that the compiler will ensure that any T that compiles does in