Why this fires¶
An operator (<, >, <=, >=, +, -, *, /, %, or unary -) is used
on a concrete struct/enum value whose type doesn't implement the required trait.
Without the trait there's no method to drive the operator, so it would crash at
runtime.
struct Tag { id: int }
let a = Tag { id: 1 }
let b = Tag { id: 2 }
let worse = a < b
// ^^^^^ error[TE823]: operator `<` requires `Tag` to implement `Ord`Fix it¶
Implement the trait. For ordering, add impl Ord with a cmp method returning
-1 / 0 / 1; <, >, <=, >= are all derived from it:
impl Ord for Tag {
fn cmp(self, other: Tag) -> int {
if self.id < other.id { 0 - 1 } else { if self.id > other.id { 1 } else { 0 } }
}
}For arithmetic, implement Add/Sub/… (method add/sub/… or @op("+")).
Notes¶
==/!=are NOT gated — every struct/enum has automatic structural equality.- Generic type parameters use
TE821(missing bound) instead.