On this page
  1. match
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

match

Prev: Sync | Up: builtins | Next: unsafe
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — match
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Language Expression) — available with zero use imports.

Syntax & Overview

match value { Pattern => expr, ... }

Exhaustive structural pattern matching over enums, tuples, slices, ranges, and guards.

Notes & Pitfalls

Example

enum Command { Quit, Move(i32, i32) }

fn describe(cmd: Command) -> &'static str {
    match cmd {
        Command::Quit => "bye",
        Command::Move(0, 0) => "stay",
        Command::Move(x, _) if x > 0 => "east",
        Command::Move(..) => "other",
    }
}

fn main() {
    assert_eq!(describe(Command::Move(5, 0)), "east");
}

Linked from

Related notes