How to Compare Structs in Rust (PartialEq, Eq)

Derive PartialEq and Eq traits on your struct to enable equality comparisons using == and != operators.

Add the PartialEq and Eq traits to your struct using the #[derive] attribute to enable equality comparisons with == and !=.

#[derive(PartialEq, Eq)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let a = Point { x: 1, y: 2 };
    let b = Point { x: 1, y: 2 };
    assert_eq!(a, b);
}