← Back to Rust Course | Chapter 3: Control Flow | Lesson 5 of 7

Pattern Matching with match

match lets your program compare a value against several possible options and run different code for each one.
Syntax
rust
match value {
    pattern1 => expression,
    pattern2 | pattern3 => expression,
    low..=high => expression,
    _ => default_expression,
}

Basic match

match compares a value against a list of patterns in order and executes the code for the first pattern that matches. It is Rust's primary tool for branching on more than two possibilities.

Example: Basic match

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `day`, set to `3`
    let day = 3;
    // Match on `day` and branch by pattern
    match day {
        1 => println!("Monday"),
        2 => println!("Tuesday"),
        3 => println!("Wednesday"),
        _ => println!("Some other day"),
    }
}

Exhaustiveness

The Rust compiler requires every match to cover all possible values of the matched type. The _ wildcard pattern is a catch-all that satisfies this requirement for any values not explicitly listed.

Example: Exhaustiveness

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `letter_grade`, set to `'B'`
    let letter_grade = 'B';
    // Match on `letter_grade` and branch by pattern
    match letter_grade {
        'A' => println!("Excellent"),
        'B' => println!("Good"),
        'C' => println!("Average"),
        _ => println!("Needs improvement"),
    }
}

Matching Multiple Values

The | operator inside a pattern lets a single match arm handle several distinct values at once, avoiding repeated arms with identical code.

Example: Matching Multiple Values

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `n`, set to `4`
    let n = 4;
    // Match on `n` and branch by pattern
    match n {
        1 | 3 | 5 | 7 => println!("{} is odd (from list)", n),
        2 | 4 | 6 | 8 => println!("{} is even (from list)", n),
        _ => println!("Out of range"),
    }
}

match as an Expression

Like if, match is an expression and can produce a value directly, which is commonly assigned to a variable instead of duplicating logic across arms.

Example: match as an Expression

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `number`, set to `6`
    let number = 6;
    // Declare an immutable variable `description`; its value comes from the block below
    let description = match number % 2 {
        0 => "even",
        _ => "odd",
    };
    // Print "{} is {}" to the console, substituting in `number, description` (with a trailing newline)
    println!("{} is {}", number, description);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that match in Rust must be exhaustive -- every possible value of the type must be covered, or it won't compile.
  2. Using _ too early in a match, accidentally catching cases that should have been handled by a more specific arm listed after it.
  3. Trying to fall through between match arms like a C switch statement -- Rust's match arms never fall through.
Chapter Summary
  • match compares a value against a series of patterns and runs the code for the first one that matches.
  • Matches must be exhaustive; the _ wildcard pattern catches any remaining cases.
  • Multiple values can share one arm using the | pattern, e.g. 1 | 2 => ....
  • match is an expression, so it can produce a value just like if.
🔒

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.