Code Example Template
Deep-Dive System Documentation
If Let: Concise State Unwrapping
When you are only interested in a single variant (such as unpacking an Option<T> or a specific Result<T, E>), standard match can feel boilerplate-heavy because you are forced to write a trailing wildcard branch _ => {}.
if let solves this by combining a pattern match and a conditional test into a concise block.
Standard Refactoring Example
Without if let:
let active_app: Option<String> = Some("Chrome".to_string());
match active_app {
Some(name) => log_app(name),
_ => {} // Boilerplate wildcard
}
With if let:
if let Some(name) = active_app {
log_app(name);
}
Mixing with Else
You can also append an else branch, which behaves identically to the fallback wildcard arm:
if let Some(name) = active_app {
focus_window(name);
} else {
focus_desktop();
}
Quick Reference Guide
if let Some(x) = opt { println!("{}", x); }