Dup Goto 📝

EnumsNotBooleans

PT2/lang/rust/examples 11-09 23:38:31
To Pop
22 lines, 69 words, 490 chars Saturday 2024-11-09 23:38:31

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);
}