100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

if let and while let in Rust

Simplify single-pattern matching in Rust using the concise if let and while let constructs.

Control FlowIntermediate7 min readJul 8, 2026
Analogies

Introduction

if let and while let are concise alternatives to match when you only care about a single pattern and want to ignore all other cases. They are especially common when working with Option and Result, where writing a full match with a throwaway _ => {} arm can feel verbose for a simple check. if let runs a block once if the pattern matches, while while let repeats the loop body for as long as the pattern continues to match.

🏏

Cricket analogy: Instead of writing a full match-based review for every possible dismissal type, a commentator uses a quick if it's a wicket check when they only care about that one outcome, skipping the exhaustive rundown of every other way the ball could go.

Syntax

rust
if let pattern = value {
    // runs once if value matches pattern
} else {
    // optional: runs if it does not match
}

while let pattern = value_expr {
    // repeats as long as value_expr matches pattern
}

Explanation

if let Some(x) = some_option checks whether some_option matches the Some pattern; if it does, x is bound to the inner value inside the block. This is equivalent to a match with a Some(x) => { ... } arm and a _ => {} arm, but with far less boilerplate for the common single-case scenario. It also supports an optional else branch for the non-matching case, similar to a two-armed match. while let works the same way but loops: on each iteration it re-evaluates the expression and continues only if the pattern still matches, stopping automatically the moment it does not — a classic use case is draining a stack or queue with while let Some(top) = stack.pop() { ... }.

🏏

Cricket analogy: if let Some(x) = some_option is like a scorer checking if there was a boundary and, only if true, extracting the exact runs scored to announce, with an else branch for no boundary this ball; while let mirrors a runner repeatedly checking while there's still a delivery left in the over and processing each one until the over ends, draining deliveries one at a time.

Example

rust
fn main() {
    let maybe_number: Option<i32> = Some(42);

    if let Some(value) = maybe_number {
        println!("Got a value: {}", value);
    } else {
        println!("No value present");
    }

    // draining a stack with while let
    let mut stack = vec![1, 2, 3];
    while let Some(top) = stack.pop() {
        println!("popped {}", top);
    }
    println!("stack is now empty: {:?}", stack);
}

Output

text
Got a value: 42
popped 3
popped 2
popped 1
stack is now empty: []

Key Takeaways

  • if let is shorthand for a match with one meaningful pattern and an ignored fallback.
  • if let supports an optional else block for the non-matching case.
  • while let loops as long as the given pattern keeps matching, stopping automatically otherwise.
  • A common idiom is while let Some(x) = iterator.next() or stack.pop() to drain a collection.
  • Unlike match, if let and while let are not exhaustive — they intentionally ignore non-matching cases.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#IfLetAndWhileLetInRust#Let#While#Syntax#Explanation#Loops#StudyNotes#SkillVeris