知识卡片

普通读书笔记卡 · 6074.d.1.a.3.b.2

派生排序依赖声明次序

理解等级:中级

结构体派生Ord或PartialOrd时按字段声明次序作词典式比较,先出现的不同字段决定结果;枚举也遵循其相应顺序规则。若业务优先级与声明次序不同,就应手写比较或重新设计,且同步保持相等与排序契约一致。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 2. Traits PartialOrd and Ord · 查看原书依据

      lexicographically in the order they’re defined, so if this isn’t correct, you’ll need to implement the traits manually (or reorder the fields). Unlike PartialEq, the PartialOrd trait does correspond to a variety of real situations. For example, it could be used to express a subset relationship among collections:2 {1, 2} is a subset of {1, 2, 4}, but {1, 3} i

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《PartialOrd and Ord》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u061-ch02-008)。 - 结论依据:原文在“lexicographically”附近的定义、例子或代码支撑本卡核心说明:结构体派生Ord或PartialOrd时按字段声明次序作词典式比较,先出现的不同字段决定结果;枚举也遵循其相应顺序规则。边界按已读前提解释;示例未执行。 - 原始内容:lexicographically in the order they’re defined, so if this isn’t correct, you’ll need to implement the traits manually (or reorder the fields). Unlike PartialEq, the PartialOrd trait does correspond to a variety of real situations. For example, it could be used to express a subset relationship among collections:2 {1, 2} is a subset of {1, 2, 4}, but {1, 3} i