Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compare structs for partial equality - PartialEq

  • struct

  • PartialEq

  • PartialEq

  • the trait Eq is not implemented for f32

#[derive(PartialEq)]
struct Thing {
    name: String,
    number: i32,
    float: f32,
}

fn main() {
    let a = Thing {
        name: String::from("Foo"),
        number: 42,
        float: 1.0,
    };

    let b = Thing {
        name: String::from("Foo"),
        number: 42,
        float: 1.0,
    };

    let c = Thing {
        name: String::from("Foo1"),
        number: 42,
        float: 1.0,
    };

    println!("{}", a == b);
    println!("{}", a == c);

    // must implement `PartialOrd`
    // println!("{}", a < c);
}
true
false