Dup Ver Goto 📝

EnumsNotBooleans

PT2/lang/rust/examples does not exist
To
22 lines, 69 words, 490 chars Page 'EnumsNotBooleans' does not exist.

The idea here is to use an enum instead of a boolean. This allows the type system to prevent a true/false value intended for one purpose inadvertently being used for another.

enum Flibble {
    Fry,
    DontFry
}

fn hex_vision(whether: Flibble) {
    match whether {
        Flibble::Fry => { println!("Fry little piggy"); },
        Flibble::DontFry => { println!("You're safe for now"); }
    };
}

fn main() {
    hex_vision(Flibble::Fry);
    hex_vision(Flibble::DontFry);
}