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
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
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
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
1. What is the main advantage of if let over a full match when handling a single pattern?
2. What does `while let Some(top) = stack.pop() { ... }` do?
3. Can if let be paired with an else branch?
4. Is while let exhaustive like match?
Was this page helpful?
You May Also Like
match Expressions in Rust
Master Rust's exhaustive match expression for pattern matching on values, enums, tuples, and ranges.
The Option Type in Rust
Understand how Rust's Option<T> enum replaces null to represent optional values safely.
if-else Expressions in Rust
Learn how Rust's if-else works as an expression, requires bool conditions, and can return values directly.
Loops in Rust (loop, while, for)
Understand Rust's three loop constructs — loop, while, and for — and how loop can return a value with break.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics