if let and while let
Concise matching.
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}");
}
}All lessons in this course
- match Deep Dive
- if let and while let
- Binding with @
- Destructuring