Rust Match

Rust provides a powerful feature called pattern matching through the match expression.

The match expression allows you to compare a value against different patterns and run code depending on which pattern matches. This is often more expressive and safer than long chains of if / else if.

The Basic syntax of match in Rust #

The general form of match looks like this:

match value {
    pattern1 => { /* code runs if pattern1 matches */ }
    pattern2 => { /* code runs if pattern2 matches */ }
    _ => { /* fallback code if nothing else matches */ }
}
Code language: Rust (rust)
  • value: The expression you want to match against.
  • pattern: A specific condition or value you want to check.
  • _: A catch-all pattern (like “else”) that matches anything not handled above.

Matching Integers #

fn main() {
    let number = 3;

    match number {
        1 => println!("One!"),
        2 => println!("Two!"),
        3 => println!("Three!"),
        _ => println!("Something else!"),
    }
}
Code language: Rust (rust)

Output:

Three!Code language: Rust (rust)

In this example:

  • If number is 1, it prints "One!".
  • If 2, it prints "Two!".
  • If 3, it prints "Three!".
  • Otherwise, it falls back to the _ case.

Matching Multiple Values #

You can match multiple patterns using | (OR):

fn main() {
    let number = 7;

    match number {
        1 | 2 | 3 => println!("It's small (1, 2, or 3)"),
        7 | 13 => println!("A lucky number!"),
        _ => println!("Something else"),
    }
}Code language: Rust (rust)

Output:

A lucky number!Code language: Rust (rust)

Matching Ranges of Values #

You can match ranges with ..=:

fn main() {
    let age = 16;

    match age {
        0..=12 => println!("Child"),
        13..=19 => println!("Teenager"),
        20..=64 => println!("Adult"),
        _ => println!("Senior"),
    }
}
Code language: Rust (rust)

Output:

TeenagerCode language: Rust (rust)

Binding Values with match #

You can also bind matched values to a variable:

fn main() {
    let number = 42;

    match number {
        n @ 1..=10 => println!("Between 1 and 10, got {}", n),
        n @ 11..=100 => println!("Between 11 and 100, got {}", n),
        _ => println!("Something else"),
    }
}
Code language: Rust (rust)

Output:

Between 11 and 100, got 42Code language: Rust (rust)

Here, n @ 11..=100 means: if the value is in that range, bind it to n.

Using match as an Expression #

Just like if, the match expression returns a value:

fn main() {
    let number = 5;

    let message = match number {
        1 => "One",
        2 | 3 | 5 | 7 => "A prime number",
        _ => "Something else",
    };

    println!("Number {} is: {}", number, message);
}Code language: Rust (rust)

Output:

Number 5 is: A prime numberCode language: Rust (rust)

Note that each arm of match must produce the same type of value (here, &str).

Nested Matching #

You can even nest match statements:

fn main() {
    let number = Some(7);

    match number {
        Some(n) => match n {
            1 => println!("Got One"),
            _ => println!("Got Something Else: {}", n),
        },
        None => println!("Got Nothing"),
    }
}
Code language: Rust (rust)

Output:

Got Something Else: 7Code language: Rust (rust)

Summary #

  • match lets you branch based on patterns.
  • Patterns can match:
    • Specific values
    • Multiple values (|)
    • Ranges (..=)
    • And even bind values to variables.
  • Always cover all possibilities; use _ as a catch-all.
  • match is also an expression, meaning it can return values.

With match, you can replace many messy if / else chains with cleaner, safer code. Later, when you learn about enums, match becomes even more powerful.

Was this Helpful ?