知识卡片
全序比较总能返回次序
部分序可能遇到无可比较的值,partial_cmp 用 None 表示这种情况。需要让任意两项拥有确定次序时用 Ord。派生的比较按字段或变体声明次序工作,领域优先级不匹配时需明确比较策略。
相关卡片 [[比较结果交给枚举]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 附录 C · 可派生的 trait PartialEq and Eq for Equality Comparisons · 查看原书依据
The Ord trait allows you to know that for any two values of the annotated type, a valid ordering will exist. The Ord trait implements the cmp method, which returns an Ordering rather than an Option<Ordering> because a valid ordering will always be possible. You can only apply the Ord trait to types that also implement PartialOrd and Eq (and Eq requires PartialEq).
参考来源
- 位置:《The Rust Programming Language(第2版)》附录 C · 可派生的 trait(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/b03.xhtml`,块 0029)
- 结论依据:原文对比 cmp 的 Ordering 与 partial_cmp 的 Option<Ordering>。
- 原始内容:The Ord trait allows you to know that for any two values of the annotated type, a valid ordering will exist. The Ord trait implements the cmp method, which returns an Ordering rather than an Option<Ordering> because a valid ordering will always be possible. You can only apply the Ord trait to types that also implement PartialOrd and Eq (and Eq requires PartialEq).