while let

A while let loop is a combination of a while loop and a let statement. It allows you to execute the loop body only if the pattern matches.

    let mut option = Option::Some(0_usize);

    // "while `let` destructures `option` into `Some(i)`:
    // evaluate the block (`{}`), else `break`
    while let Option::Some(i) =
        option {
            if i > 0 {
                println!("Greater than 0, break...");
                option = Option::None;
            } else {
                println!("`i` is `{:?}`. Try again.", i);
                option = Option::Some(i + 1);
            }
        }

See also

while

Last change: 2024-04-10, commit: a0d03de