Rust Pattern Matching Cheat Sheet
Covers match expressions, if let and while let, destructuring structs and enums, match guards, and binding patterns in Rust.
2 PagesBeginnerApr 8, 2026
match Basics
Exhaustive matching over values, ranges, and enum variants.
rust
fn describe(n: i32) -> &'static str { match n { 0 => "zero", 1 | 2 | 3 => "small", // multiple patterns 4..=9 => "medium", // inclusive range n if n < 0 => "negative", // match guard _ => "large", // catch-all }}enum Coin { Penny, Nickel, Dime, Quarter(String), // state name}fn value_in_cents(coin: &Coin) -> u32 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter(state) => { println!("Quarter from {}!", state); 25 } }}
Destructuring
Pulling apart structs, tuples, and enums into named bindings.
rust
struct Point { x: i32, y: i32 }let p = Point { x: 0, y: 7 };let Point { x, y } = p;println!("{} {}", x, y);// Destructuring in match, with nested enumsenum Shape { Circle { radius: f64 }, Rectangle { width: f64, height: f64 },}fn area(shape: &Shape) -> f64 { match shape { Shape::Circle { radius } => std::f64::consts::PI * radius * radius, Shape::Rectangle { width, height } => width * height, }}// Tuple destructuringlet (a, b, c) = (1, 2, 3);// Ignoring parts with `..`let Point { x, .. } = Point { x: 1, y: 2 };
Match Guards & @ Bindings
Adding extra conditions and capturing matched values by name.
rust
let pair = (2, -2);match pair { (x, y) if x + y == 0 => println!("These sum to zero"), (x, _) if x % 2 == 0 => println!("First is even"), _ => println!("No match"),}// @ bindings: test a value AND bind it to a namelet msg_id = 5;match msg_id { id @ 3..=7 => println!("Found id in range: {}", id), _ => println!("Out of range"),}
Pattern Syntax Reference
Symbols used across match arms and let/if let bindings.
- _- Wildcard; matches anything without binding a value
- ..- Ignores remaining fields/elements in a struct, tuple, or slice pattern
- |- Or-pattern; matches if any of the listed patterns match
- a..=b- Inclusive range pattern (works for char and numeric types)
- name @ pattern- Binds the matched value to `name` while also testing it against `pattern`
- ref / ref mut- Binds by reference instead of moving the matched value (rarely needed post-NLL)
- Some(x) / None- Standard destructuring of Option in match arms
if let / while let
Concise matching for a single pattern of interest.
rust
let config_max: Option<u8> = Some(3);// if let: handle one pattern, ignore the restif let Some(max) = config_max { println!("Max is {}", max);} else { println!("No max set");}// while let: loop as long as the pattern keeps matchinglet mut stack = vec![1, 2, 3];while let Some(top) = stack.pop() { println!("{}", top);}// let-else (Rust 1.65+): bind or divergelet Some(max) = config_max else { panic!("config_max should be set");};
Pro Tip
match is exhaustive by compiler enforcement — if you add a new enum variant later, every match on it fails to compile until you handle the new case, making enums+match a powerful tool for safe refactors.
Was this cheat sheet helpful?
Explore Topics
#RustPatternMatching#RustPatternMatchingCheatSheet#Programming#Beginner#MatchBasics#Destructuring#MatchGuardsBindings#PatternSyntaxReference#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance