src/std-traits/operators.md
Operator overloading is implemented via traits in std::ops:
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
#[derive(Debug, Copy, Clone)]
struct Point {
x: i32,
y: i32,
}
impl std::ops::Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self { x: self.x + other.x, y: self.y + other.y }
}
}
fn main() {
let p1 = Point { x: 10, y: 20 };
let p2 = Point { x: 100, y: 200 };
println!("{p1:?} + {p2:?} = {:?}", p1 + p2);
}
Discussion points:
Add for &Point. In which situations is that useful?
Add:add consumes self. If type T for which you are overloading
the operator is not Copy, you should consider overloading the operator for
&T as well. This avoids unnecessary cloning on the call site.Output an associated type? Could it be made a type parameter of the
method?
Output) are controlled by the implementer of a
trait.Add for two different types, e.g.
impl Add<(i32, i32)> for Point would add a tuple to a Point.The Not trait (! operator) is notable because it does not convert the
argument to bool like the same operator in C-family languages; instead, for
integer types it flips each bit of the number, which, arithmetically, is
equivalent to subtracting the argument from -1: !5 == -6.