How to Use std

:ops for Operator Overloading

Overload operators in Rust by implementing specific traits from the std::ops module on your custom types.

You overload operators in Rust by implementing specific traits from the std::ops module on your custom types. For example, to make your Point struct support addition, you implement the Add trait and define the logic inside the add method.

use std::ops::Add;

struct Point {
    x: i32,
    y: i32,
}

impl Add for Point {
    type Output = Point;

    fn add(self, other: Point) -> Point {
        Point {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

fn main() {
    let p1 = Point { x: 1, y: 2 };
    let p2 = Point { x: 3, y: 4 };
    let p3 = p1 + p2; // Uses the overloaded + operator
    println!("({},{})", p3.x, p3.y);
}