How to Use if let and while let in Rust

Use if let for single checks and while let for loops on optional values in Rust.

Use if let to run code once when a pattern matches, and while let to loop while a pattern matches.

fn main() {
    let optional = Some(5);

    if let Some(value) = optional {
        println!("Found: {value}");
    }

    let mut counter = 3;
    while let Some(n) = if counter > 0 { Some(counter) } else { None } {
        println!("Count: {n}");
        counter -= 1;
    }
}