Pattern Matching Multiple Variants in Rust

August 16, 2019

One of my favorite features of Rust is the enum data type and the pattern matching capabilities of match. It allows me to be sure that I have covered every case and if not, the compiler will let me know.

For example,

use TrafficLightColor::*;

enum TrafficLightColor {
    Red,
    Yellow,
    Green
}

fn main() {
  println!("Can I go?");
  
  let current_light_color = Red;
  match current_light_color {
      Red => println!("No!"),
      Green => println!("Yes!"),
      Yellow => println!("Yes!"),
  };
}
$ cargo run
Can I go? 
No!

But what if I need to do the same thing for two variants? Rust allows you to match on multiple variants with the | operator.

use TrafficLightColor::*;

enum TrafficLightColor {
    Red,
    Yellow,
    Green
}

fn main() {
  println!("Can I go?");
  
  let current_light_color = TrafficLightColor::Yellow;
  match current_light_color {
      Red => println!("No!"),
      Green | Yellow => println!("Yes!"),
  };
}
$ cargo run
Can I go?
Yes!

Here's a REPL demonstrating this.

Made it this far? Have a go at the game of snake