if let and while let
Concise matching.
if let and while let is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Concise Matching
Sometimes you care about just one pattern. if let and while let give you concise matching without a full match.
The if let Form
if let runs a block only when a value matches a pattern, binding any captured data.
fn main() {
let maybe = Some(10);
if let Some(n) = maybe {
println!("value is {n}");
}
}if let vs match
if let is sugar for a match with one interesting arm and a _ => () fallthrough. Use it when the other cases need no handling.
fn main() {
let config: Option<i32> = None;
// match equivalent would have an empty _ arm
if let Some(v) = config {
println!("configured: {v}");
}
println!("done");
}Adding an else
Attach an else to handle the non-matching case.
fn main() {
let result: Result<i32, String> = Err("boom".to_string());
if let Ok(v) = result {
println!("ok: {v}");
} else {
println!("failed");
}
}let else
let ... else binds when the pattern matches, otherwise runs a diverging block (like return). Great for early exits.
fn parse(s: &str) -> i32 {
let Ok(n) = s.parse::<i32>() else {
println!("not a number");
return 0;
};
n * 2
}
fn main() {
println!("{}", parse("21"));
println!("{}", parse("oops"));
}The while let Form
while let keeps looping as long as a value matches a pattern. It's perfect for draining things like stacks.
fn main() {
let mut stack = vec![1, 2, 3];
while let Some(top) = stack.pop() {
println!("popped {top}");
}
}Looping Over Iterators
while let can iterate by repeatedly calling next, stopping when it returns None.
fn main() {
let mut iter = [10, 20, 30].into_iter();
while let Some(x) = iter.next() {
println!("{x}");
}
}Guards with if let
Combine a guard by adding an && condition (Rust 2024) or nesting an if inside.
fn main() {
let value = Some(8);
if let Some(n) = value {
if n > 5 {
println!("big: {n}");
}
}
}When to Use Which
Guidelines:
if let— handle one pattern, ignore the restlet else— bind or bail out earlywhile let— loop while a pattern keeps matchingmatch— when you need to handle multiple cases
Readability Tradeoff
These forms reduce boilerplate but can hide cases. If you find yourself handling several patterns, switch back to a full match for clarity.
Nesting if let
You can chain optional values by nesting if let, unwrapping one layer at a time.
fn main() {
let outer: Option<Option<i32>> = Some(Some(99));
if let Some(inner) = outer {
if let Some(value) = inner {
println!("value is {value}");
}
}
}Quick Check
Which construct repeatedly runs a loop body while a value keeps matching a pattern?
Recap
You learned concise matching:
if lethandles a single pattern, optionally withelselet elsebinds or diverges earlywhile letloops while a pattern matches- Switch to
matchwhen multiple cases need handling
Frequently asked questions
Is the “if let and while let” lesson free?
Yes — the full text of “if let and while let” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “if let and while let”?
Concise matching. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “if let and while let” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- match Deep Dive
- if let and while let
- Binding with @
- Destructuring