知识卡片
trait表达共同能力
trait声明一组相关行为,使泛型代码可以要求某种能力而不固定具体结构体。方法常带self接收者,也可以是关联函数;默认实现则可提供共同逻辑。设计抽象时应先找到调用者实际需要的共同能力,不因能写trait就把每个结构体都包装一层。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Traits · 查看原书依据
Traits The Fn* traits are more flexible than bare function pointers, but they can still describe only the behavior of a single function, and even then only in terms of the function’s signature. However, they are themselves examples of another mechanism for describing behavior in Rust’s type system, the trait. A trait defines a set of related functions that s
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Traits》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u019-ch01-018)。
- 结论依据:原文在“Traits”附近的定义、例子或代码支撑本卡核心说明:trait声明一组相关行为,使泛型代码可以要求某种能力而不固定具体结构体。边界按已读前提解释;示例未执行。
- 原始内容:Traits The Fn* traits are more flexible than bare function pointers, but they can still describe only the behavior of a single function, and even then only in terms of the function’s signature. However, they are themselves examples of another mechanism for describing behavior in Rust’s type system, the trait. A trait defines a set of related functions that s