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

Enumeration and manual comparision

  • PartialEq

We can also compare variables holding enum variants, but for that to work we also need to derivede from the ParialEq trait. Basically we need to implement the operation that allows use to compare two values of this type.

#[derive(PartialEq)]
#[allow(dead_code)]
enum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday,
}

// We need the allow dead_code beacause in this example
// we did not use all the values that were listed in the enum

fn main() {
    let today = Weekday::Saturday;
    let tomorrow = Weekday::Sunday;
    let market = Weekday::Sunday;

    if market == today {
        println!("Today is market day");
    }
    if market == tomorrow {
        println!("Tomorrow is market day");
    }
}
Tomorrow is market day