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