RRust By Example
intermediateFlow Control

While Let

Loop with pattern matching at the top.

While Let

Loop with pattern matching at the top.

Difficulty

Intermediate

Code

rust
fn main() {
    let mut stack = vec![1, 2, 3, 4, 5];
    while let Some(top) = stack.pop() {
        println!("popped: {}", top);
    }
}

Explanation

while let loops while a pattern matches.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the flow-control category to build a complete understanding of this topic.

More Flow Control Examples