Skip to content

Operators for your own types

a + b on a struct or enum calls its Add.add(&a, &b); -, *, /, % use Sub, Mul, Div, Rem, and unary - uses Neg. These prelude traits take both sides by reference and return a new value, so an operator never moves its operands. == and != use Equal.equals when a type has it (and otherwise compare every part), and <, <=, >, >= use Order.compare, which returns a negative number, zero or a positive one. Using an operator a type does not implement says which trait it needs.