知识卡片
运算符重载保持直觉与一致
为自定义类型重载运算符时,应有自然代数含义,并使相关运算一致,例如x-y与x+(-y)应相符。别把昂贵且出乎意料的作用藏在简单符号中。运算trait按值参数会消耗非Copy输入;若支持借用运算,需另行覆盖引用组合并维护相同语义。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 2. Traits Operator Overloads · 查看原书依据
Operator Overloads The final category of standard traits relates to operator overloads, where Rust allows various built-in unary and binary operators to be overloaded for user-defined types, by implementing various standard traits from the std::ops module. These traits are not derivable and are typically needed only for types that represent “algebraic” objec
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第2章《Operator Overloads》(spine6;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch02.html;u065-ch02-011)。
- 结论依据:原文在“Operator Overloads”附近的定义、例子或代码支撑本卡核心说明:为自定义类型重载运算符时,应有自然代数含义,并使相关运算一致,例如x-y与x+(-y)应相符。边界按已读前提解释;示例未执行。
- 原始内容:Operator Overloads The final category of standard traits relates to operator overloads, where Rust allows various built-in unary and binary operators to be overloaded for user-defined types, by implementing various standard traits from the std::ops module. These traits are not derivable and are typically needed only for types that represent “algebraic” objec