AmitKumarDas / Decisions

Apache License 2.0
10 stars 3 forks source link

Rust: Enum and `==` do not work as you think #127

Open AmitKumarDas opened 5 years ago

AmitKumarDas commented 5 years ago

:nerd_face: if let is used along with single equal & not double equal :bulb: if let zbc = xyz :nerd_face: if let is an alternative to match & guard

// This enum purposely doesn't #[derive(PartialEq)],
// neither we implement PartialEq for it. That's why comparing Foo::Bar==a fails below.
enum Foo {Bar}

fn main() {
    let a = Foo::Bar;

    // Variable a matches Foo::Bar
    if Foo::Bar == a {
    // ^-- this causes a compile-time error. Use `if let` instead.
        println!("a is foobar");
    }
}