知识卡片
标记trait承载语义承诺
不增加方法的标记trait仍能表达约束。StableSort:Sort表示实现StableSort必须也实现Sort,并额外承诺相等元素保持相对次序;编译器检查trait约束,不会自动证明排序稳定性。supertrait是实现条件,不应理解为传统类的字段或实现继承。
结构图:
classDiagram
class Sort {
<<trait>>
+sort(&mut self)
}
class StableSort {
<<marker trait>>
}
StableSort ..> Sort : supertrait约束
箭头表示要求同时实现的能力,不表示StableSort继承某个具体排序实现。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 1. Types Traits · 查看原书依据
StableSort: Sort {} A marker trait has no functions, but an implementation still has to declare that it is implementing the trait—which acts as a promise from the implementer: “I solemnly swear that my implementation sorts stably.” Code that relies on a stable sort can then specify the StableSort trait bound, relying on the honor system to preserve its invar
参考来源
- 位置:《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-019)。
- 结论依据:原文在“StableSort”附近的定义、例子或代码支撑本卡核心说明:不增加方法的标记trait仍能表达约束。边界按已读前提解释;示例未执行。
- 原始内容:StableSort: Sort {} A marker trait has no functions, but an implementation still has to declare that it is implementing the trait—which acts as a promise from the implementer: “I solemnly swear that my implementation sorts stably.” Code that relies on a stable sort can then specify the StableSort trait bound, relying on the honor system to preserve its invar