← Back to Rust Course | Chapter 8: Enums & Pattern Matching | Lesson 6 of 7

Nested Patterns

Nested patterns let you look deep inside a value made of other values, all in a single match arm.

Matching a Struct Inside an Enum

A pattern can reach directly into a struct that is wrapped inside an enum variant, extracting its fields in the same match arm.

Example: Matching a Struct Inside an Enum

markup
struct Point {
    x: i32,
    y: i32,
}

enum Shape {
    Dot(Point),
}

fn main() {
    let shape = Shape::Dot(Point { x: 3, y: 4 });
    match shape {
        Shape::Dot(Point { x, y }) => println!("Dot at ({}, {})", x, y),
    }
}

Matching Nested Tuples

Tuples can be nested inside each other, and a single pattern can destructure all the levels at once, binding names to whichever inner values you need.

Example: Matching Nested Tuples

markup
fn main() {
    let nested = (1, (2, 3));
    let (a, (b, c)) = nested;
    println!("a={}, b={}, c={}", a, b, c);
}

Matching an Option Inside an Enum Variant

Patterns can drill through multiple layers of wrapping, like an Option stored inside an enum variant, extracting the innermost value directly.

Example: Matching an Option Inside an Enum Variant

markup
enum Response {
    Data(Option<i32>),
}

fn main() {
    let resp = Response::Data(Some(99));
    match resp {
        Response::Data(Some(value)) => println!("Got data: {}", value),
        Response::Data(None) => println!("No data"),
    }
}

Keeping Nesting Readable

While Rust allows arbitrarily deep nested patterns, it is good practice to keep them shallow enough to read at a glance, splitting very deep structures into smaller matches or helper functions when needed.

Example: Keeping Nesting Readable

markup
struct Address {
    city: String,
}

struct Person {
    address: Address,
}

fn main() {
    let person = Person { address: Address { city: String::from("Kyoto") } };
    let Person { address: Address { city } } = person;
    println!("City: {}", city);
}
Common Mistakes
  1. Trying to match nested enum variants without wrapping the inner pattern properly, causing a syntax error.
  2. Writing overly deep nested patterns that become hard to read, when splitting into a helper function or several matches would be clearer.
  3. Forgetting a nested Option<Option<T>> or similar double-wrapped structure needs two layers of pattern matching to fully unwrap.
Chapter Summary
  • Patterns can nest inside each other, matching structures like Some(Point { x, y }) in a single arm.
  • Nested patterns can combine enums, structs, and tuples together in one match expression.
  • Deeply nested data can be destructured directly, avoiding a chain of separate match or if-let statements.
  • Overly deep nesting can hurt readability, so keeping patterns reasonably shallow is a good practice.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.