Skip to content

Rust Pattern Matching — Advanced Patterns, Guards, and Destructuring

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Rust Pattern Matching. We cover key concepts, practical examples, and best practices to help you master this topic.

Rust pattern matching includes destructuring structs/enums/tuples, match guards, @ bindings, and wildcard patterns for complex data extraction.

What You'll Learn

  • Destructuring patterns
  • Match guards
  • @ bindings
  • Refutability

Why It Matters

Pattern matching is a Rust superpower. DodaZIP uses destructuring for archive entry Parsing.

Real-World Use

Complex data extraction, state machine transitions, error handling, configuration parsing.

struct Point { x: i32, y: i32 }
enum Shape { Circle(Point, i32), Rectangle(Point, Point) }

fn main() {
    let p = Point { x: 10, y: 20 };
    let Point { x, y } = p;
    println!("({}, {})", x, y);

    let shape = Shape::Circle(Point { x: 0, y: 0 }, 5);
    match shape {
        Shape::Circle(center, radius) => {
            println!("Circle at ({}, {}) radius {}", center.x, center.y, radius);
        }
        Shape::Rectangle(p1, p2) => {
            println!("Rect from ({}, {}) to ({}, {})", p1.x, p1.y, p2.x, p2.y);
        }
    }

    let num = Some(42);
    match num {
        Some(x) if x > 40 => println!("Big: {}", x),
        Some(x) => println!("Small: {}", x),
        None => println!("None"),
    }

    let val = 5;
    match val {
        1..=5 => println!("Small"),
        6..=10 => println!("Medium"),
        _ => println!("Large"),
    }

    match val {
        n @ 1..=5 => println!("Bound: {}", n),
        _ => println!("Other"),
    }
}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro