0Pricing
Learn Rust Coding · Lesson

match Deep Dive

Patterns and guards.

match Deep Dive is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The match Expression

match compares a value against a series of patterns and runs the arm of the first match. It is an expression, so it returns a value.

fn main() {
    let n = 3;
    let label = match n {
        1 => "one",
        2 => "two",
        3 => "three",
        _ => "many",
    };
    println!("{label}");
}

Exhaustiveness

A match must cover every possible value. The wildcard _ catches anything not matched by earlier arms, ensuring exhaustiveness.

fn main() {
    let x = 42;
    match x {
        0 => println!("zero"),
        _ => println!("nonzero"),
    }
}

Matching Multiple Values

Use | to match several patterns in one arm.

fn main() {
    let c = 'e';
    match c {
        'a' | 'e' | 'i' | 'o' | 'u' => println!("vowel"),
        _ => println!("consonant"),
    }
}

Range Patterns

Match a range of values with ..= (inclusive). Great for grouping numbers or characters.

fn main() {
    let score = 85;
    let grade = match score {
        90..=100 => 'A',
        80..=89 => 'B',
        70..=79 => 'C',
        _ => 'F',
    };
    println!("Grade: {grade}");
}

Match Guards

A guard is an extra if condition on an arm. The arm matches only when the pattern fits and the guard is true.

fn main() {
    let pair = (2, -2);
    match pair {
        (x, y) if x + y == 0 => println!("sum is zero"),
        (x, _) if x % 2 == 0 => println!("first is even"),
        _ => println!("no rule"),
    }
}

Binding in Patterns

Patterns can bind matched parts to variables for use in the arm body.

fn main() {
    let msg = Some(7);
    match msg {
        Some(n) => println!("got {n}"),
        None => println!("nothing"),
    }
}

Matching Enums

match shines with enums, especially when variants carry data.

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

fn area(s: Shape) -> f64 {
    match s {
        Shape::Circle(r) => 3.14159 * r * r,
        Shape::Rectangle(w, h) => w * h,
    }
}

fn main() {
    println!("{}", area(Shape::Rectangle(3.0, 4.0)));
}

Ignoring with _ and ..

Use _ to ignore a single value and .. to ignore the rest of a tuple or struct.

fn main() {
    let triple = (1, 2, 3);
    match triple {
        (first, ..) => println!("first is {first}"),
    }
}

Returning From match

Because match is an expression, you can assign its result or return it directly. Every arm must yield the same type.

fn describe(n: i32) -> &'static str {
    match n.cmp(&0) {
        std::cmp::Ordering::Less => "negative",
        std::cmp::Ordering::Equal => "zero",
        std::cmp::Ordering::Greater => "positive",
    }
}

fn main() {
    println!("{}", describe(-5));
}

Arm Order Matters

Arms are checked top to bottom; the first matching one wins. Put specific patterns before general ones and the wildcard last.

Binding Ranges with @

Combine a range with the @ operator to test a value and keep its exact contents in the same arm.

fn main() {
    let n = 42;
    match n {
        small @ 0..=9 => println!("single digit: {small}"),
        big @ 10..=99 => println!("two digits: {big}"),
        _ => println!("large"),
    }
}

Quick Check

What is the purpose of a match guard like (x, y) if x > y?

Recap

You went deep on match:

  • It is an exhaustive expression returning a value
  • | matches multiple patterns; ..= matches ranges
  • Guards add extra if conditions
  • Patterns bind data; _ and .. ignore parts
  • Arm order matters — first match wins

Frequently asked questions

Is the “match Deep Dive” lesson free?

Yes — the full text of “match Deep Dive” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “match Deep Dive”?

Patterns and guards. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “match Deep Dive” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. match Deep Dive
  2. if let and while let
  3. Binding with @
  4. Destructuring
← Back to Learn Rust Coding