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

if_let.rs

In rust you can do if condition

fn main() {
    let condition = true;
    let condition = false;
    let condition = 12414214 + 2412; // Bad b/c IFLET needs a BOOL!
    let number = if condition { 5 } else { 6 };

    println!("The value of number is: {number}");
}

Or...

fn main() {
    let letter: Option<i32> = None;
    if let Some(i) = letter {
        println!("Matched {:?}!", i);
    } else {
        println!("No number, using letter instead!");  // This runs
    }
}

Or with a ENUM!!!!!!

#[derive(Debug)]
enum Foo {
    Bar,
    Baz,
    Qux(u32),
}

fn main() {
    let c = Foo::Qux(100);
    if let Foo::Qux(value) = c {
        println!("c is {}", value);  // Outputs: c is 100
    }
}