ProgramGuru

Match expressions and patterns

Match expressions and patterns

A school can divide valid marks into several bands. Match compares a value with patterns and chooses the first matching arm. A pattern describes a shape or range to recognize, rather than an arbitrary expression that always returns true or false.

Inclusive ranges use ..=. An underscore pattern matches remaining values. Match must cover all possible inputs, and its arms must return compatible types when used as a value. Match guards add a condition after a pattern when recognition needs more than shape alone.

What you’ll learn

  • Match grading ranges in the intended order.
  • Account for values outside the accepted mark range.

Match expressions and patterns in action

  1. Mark: 82

    Eighty-two does not match the first range but matches the second.

    Mark: 82
  2. First range misses

    That arm supplies Target met.

    First range misses
  3. Second range selects label

    The final underscore handles negative values and values above 100, so the match covers the full integer type.

    Second range selects label
Eighty-two does not match the first range but matches the second. That arm supplies Target met. The final underscore handles negative values and values above 100, so the match covers the full integer type.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

fn main() {
  let score = 82;
  let band = match score {
    90..=100 => "Excellent",
    75..=89 => "Target met",
    0..=74 => "More practice",
    _ => "Invalid mark",
  };
  println!("{band}");
}

Expected output

Target met

Follow the execution

Eighty-two does not match the first range but matches the second. That arm supplies Target met. The final underscore handles negative values and values above 100, so the match covers the full integer type.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: What happens if score becomes 120?

Rust 2024 · complete program

fn main() {
  let score = 82;
  let band = match score {
    90..=100 => "Excellent",
    75..=89 => "Target met",
    0..=74 => "More practice",
    _ => "Invalid mark",
  };
  println!("{band}");
}

Worked answer

The remaining-value arm produces Invalid mark. It is outside every accepted range, so it should not receive a valid grading label.

Mistakes and limits to recognize

Order matters when patterns overlap. Exhaustive matching checks structural coverage, but a catch-all arm can hide newly added enum variants from review. Pattern matching is not restricted to numbers; enums and destructuring make it especially useful.

Check your understanding

What happens if score becomes 120?
Choose one answer. You can try again.

Answer and explanation

The remaining-value arm produces Invalid mark.

The remaining-value arm produces Invalid mark. It is outside every accepted range, so it should not receive a valid grading label.

Take the idea into your next program

The remaining-value arm produces Invalid mark. It is outside every accepted range, so it should not receive a valid grading label. Order matters when patterns overlap.

Further reading: the official Rust Book on match expressions and patterns.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: For loops and ranges →