pattern

Rust Repeat while a pattern matches

Use while let when each iteration obtains a value that might be absent. Here a vector acts as a stack; pop returns the last element or None. Expected output (not executed): 3 2 1 Why: Each successful pop removes the last item; None ends the loop. Common mistake: A while-let condition must make progress if you expect the loop to terminate.

Formal Syntax & Structure

rust syntaxrust
while let pattern = expression { ... }

Working Example

Example Snippetrust
fn main() {    let mut jobs = vec![1, 2, 3];    while let Some(job) = jobs.pop() {        println!("{job}");    }}
Rust Reference