liveBook · Manning

Match statement

As explained in Refactoring to Rust

A match expression is Rust's pattern-matching control-flow expression that selects and evaluates an arm based on a value's structure or variant.

A `match` expression selects an arm based on a value. With enums, each arm handles a variant; an arm can also bind data carried by that variant. section 2.5.1

Listing 2.25 Using match expressions with enums
enum FizzBuzzValue {
  Fizz,
  Buzz,
  FizzBuzz,
  NotDivisible,
}

fn main() {
  for i in 1..101 {
    print_fizzbuzz(i);
  }
}

fn print_fizzbuzz(x: i32) {
  match fizzbuzz(x) {
    FizzBuzzValue::FizzBuzz => {  #1
      println!("FizzBuzz");
    }
    FizzBuzzValue::Fizz => {
      println!("Fizz");
    }
    FizzBuzzValue::Buzz => {
      println!("Buzz");
    }
    FizzBuzzValue::NotDivisible => {
      println!("{}", x);
    }
  }
}

fn fizzbuzz(x: i32) -> FizzBuzzValue {
  if x % 3 == 0 && x % 5 == 0 {
    FizzBuzzValue::FizzBuzz
  } else if x % 3 == 0 {
    FizzBuzzValue::Fizz
  } else if x % 5 == 0 {
    FizzBuzzValue::Buzz
  } else {
    FizzBuzzValue::NotDivisible
  }
}

This FizzBuzz example matches each enum variant to the output it should print. section 2.5.1

Listing 2.26 FizzBuzzValue enum holding a number not divisible by 3 or 5
enum FizzBuzzValue {
  Fizz,
  Buzz,
  FizzBuzz,
  NotDivisible(i32),  #1
}

fn main() {
  for i in 1..101 {
    print_fizzbuzz(i);
  }
}

fn print_fizzbuzz(x: i32) {
  match fizzbuzz(x) {
    FizzBuzzValue::FizzBuzz => {
      println!("FizzBuzz");
    }
    FizzBuzzValue::Fizz => {
      println!("Fizz");
    }
    FizzBuzzValue::Buzz => {
      println!("Buzz");
    }
    FizzBuzzValue::NotDivisible(num) => {  #2
      println!("{}", num);
    }
  }
}

fn fizzbuzz(x: i32) -> FizzBuzzValue {
  if x % 3 == 0 && x % 5 == 0 {
    FizzBuzzValue::FizzBuzz
  } else if x % 3 == 0 {
    FizzBuzzValue::Fizz
  } else if x % 5 == 0 {
    FizzBuzzValue::Buzz
  } else {
    FizzBuzzValue::NotDivisible(x)  #3
  }
}

The `NotDivisible(num)` arm shows how a match can bind a value carried by an enum variant. section 2.5.1

A match can also produce a value: matching a `Result` can extract `Ok` data or return early from the function on `Err`. section 2.5.4

How Refactoring to Rust covers it

How deep it goes
Explains enum-arm selection, payload binding, and match as a value that can return early on Err.
Examples
Uses FizzBuzz variants, including a data-carrying NotDivisible variant, and matches Result with an early error return.
What you can do after
Apply it via Rust to dispatch enum variants, bind variant data, and handle Result errors with explicit early returns.
How it connects
Connects enum variants and Result error handling, and compares match with switch statements.

Also explained in

Compare how 2 books explain match statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.