if let and while let for Partial Matching

Updated

October 31, 2026

if let and while let for Partial Matching

When you only care about matching one pattern and ignoring all others, if let and while let avoid boilerplate:

fn main() {
    let config_val: Option<u32> = Some(8080);

    // if let syntax
    if let Some(port) = config_val {
        println!("Binding server to port {}", port);
    }

    // while let syntax
    let mut stack = vec![1, 2, 3];
    while let Some(top) = stack.pop() {
        println!("Popped: {}", top);
    }
}