Add, Mul, โ€ฆ

์—ฐ์‚ฐ์ž ์˜ค๋ฒ„๋กœ๋“œ๋Š” std::ops ํŠธ๋ ˆ์ดํŠธ์„ ํ†ตํ•ด ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค:

Operator overloading is implemented via traits in std::ops:

#[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);
}