Operators call functions on user types: + calls add, - sub, * mul, / div,
% mod, and < calls cmp. This struct or enum has no such function.
Wrong#
struct V { x: int }
fn main() { print(V { x: 1 } + V { x: 2 }) }Fixed#
struct V { x: int }
fn add(a: V, b: V): V = V { x: a.x + b.x }
fn main() { print(V { x: 1 } + V { x: 2 }) }Common causes#
- Using
+,<etc. on your own type without definingadd,cmp, ... ==is automatic for structs, enums and tuples; ordering is not (definecmp).