The match Control Flow Construct

Rust has an extremely powerful control flow construct called match that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things; Chapter 18 covers all the different kinds of patterns and what they do. The power of match comes from the expressiveness of the patterns and the fact that the compiler confirms that all possible cases are handled.

Think of a match expression as being like a coin-sorting machine: coins slide down a track with variously sized holes along it, and each coin falls through the first hole it encounters that it fits into. In the same way, values go through each pattern in a match, and at the first pattern the value “fits,” the value falls into the associated code block to be used during execution.

Speaking of coins, let’s use them as an example using match! We can write a function that takes an unknown US coin and, in a similar way as the counting machine, determines which coin it is and returns its value in cents, as shown in Listing 6-3.

  1. enum Coin {
  2.     Penny,
  3.     Nickel,
  4.     Dime,
  5.     Quarter,
  6. }
  7. fn value_in_cents(coin: Coin) -> u8 {
  8.     match coin {
  9.         Coin::Penny => 1,
  10.         Coin::Nickel => 5,
  11.         Coin::Dime => 10,
  12.         Coin::Quarter => 25,
  13.     }
  14. }
  15. fn main() {}

Listing 6-3: An enum and a match expression that has the variants of the enum as its patterns

Let’s break down the match in the value_in_cents function. First we list the match keyword followed by an expression, which in this case is the value coin. This seems very similar to a conditional expression used with if, but there’s a big difference: with if, the condition needs to evaluate to a Boolean value, but here it can be any type. The type of coin in this example is the Coin enum that we defined on the first line.

Next are the match arms. An arm has two parts: a pattern and some code. The first arm here has a pattern that is the value Coin::Penny and then the => operator that separates the pattern and the code to run. The code in this case is just the value 1. Each arm is separated from the next with a comma.

When the match expression executes, it compares the resultant value against the pattern of each arm, in order. If a pattern matches the value, the code associated with that pattern is executed. If that pattern doesn’t match the value, execution continues to the next arm, much as in a coin-sorting machine. We can have as many arms as we need: in Listing 6-3, our match has four arms.

The code associated with each arm is an expression, and the resultant value of the expression in the matching arm is the value that gets returned for the entire match expression.

We don’t typically use curly brackets if the match arm code is short, as it is in Listing 6-3 where each arm just returns a value. If you want to run multiple lines of code in a match arm, you must use curly brackets, and the comma following the arm is then optional. For example, the following code prints “Lucky penny!” every time the method is called with a Coin::Penny, but still returns the last value of the block, 1:

  1. enum Coin {
  2.     Penny,
  3.     Nickel,
  4.     Dime,
  5.     Quarter,
  6. }
  7. fn value_in_cents(coin: Coin) -> u8 {
  8.     match coin {
  9.         Coin::Penny => {
  10.             println!("Lucky penny!");
  11.             1
  12.         }
  13.         Coin::Nickel => 5,
  14.         Coin::Dime => 10,
  15.         Coin::Quarter => 25,
  16.     }
  17. }
  18. fn main() {}

Patterns That Bind to Values

Another useful feature of match arms is that they can bind to the parts of the values that match the pattern. This is how we can extract values out of enum variants.

As an example, let’s change one of our enum variants to hold data inside it. From 1999 through 2008, the United States minted quarters with different designs for each of the 50 states on one side. No other coins got state designs, so only quarters have this extra value. We can add this information to our enum by changing the Quarter variant to include a UsState value stored inside it, which we’ve done in Listing 6-4.

  1. #[derive(Debug)] // so we can inspect the state in a minute
  2. enum UsState {
  3.     Alabama,
  4.     Alaska,
  5.     // --snip--
  6. }
  7. enum Coin {
  8.     Penny,
  9.     Nickel,
  10.     Dime,
  11.     Quarter(UsState),
  12. }
  13. fn main() {}

Listing 6-4: A Coin enum in which the Quarter variant also holds a UsState value

Let’s imagine that a friend is trying to collect all 50 state quarters. While we sort our loose change by coin type, we’ll also call out the name of the state associated with each quarter so that if it’s one our friend doesn’t have, they can add it to their collection.

In the match expression for this code, we add a variable called state to the pattern that matches values of the variant Coin::Quarter. When a Coin::Quarter matches, the state variable will bind to the value of that quarter’s state. Then we can use state in the code for that arm, like so:

  1. #[derive(Debug)]
  2. enum UsState {
  3.     Alabama,
  4.     Alaska,
  5.     // --snip--
  6. }
  7. enum Coin {
  8.     Penny,
  9.     Nickel,
  10.     Dime,
  11.     Quarter(UsState),
  12. }
  13. fn value_in_cents(coin: Coin) -> u8 {
  14.     match coin {
  15.         Coin::Penny => 1,
  16.         Coin::Nickel => 5,
  17.         Coin::Dime => 10,
  18.         Coin::Quarter(state) => {
  19.             println!("State quarter from {state:?}!");
  20.             25
  21.         }
  22.     }
  23. }
  24. fn main() {
  25.     value_in_cents(Coin::Quarter(UsState::Alaska));
  26. }

If we were to call value_in_cents(Coin::Quarter(UsState::Alaska)), coin would be Coin::Quarter(UsState::Alaska). When we compare that value with each of the match arms, none of them match until we reach Coin::Quarter(state). At that point, the binding for state will be the value UsState::Alaska. We can then use that binding in the println! expression, thus getting the inner state value out of the Coin enum variant for Quarter.

Matching with Option

In the previous section, we wanted to get the inner T value out of the Some case when using Option<T>; we can also handle Option<T> using match, as we did with the Coin enum! Instead of comparing coins, we’ll compare the variants of Option<T>, but the way the match expression works remains the same.

Let’s say we want to write a function that takes an Option<i32> and, if there’s a value inside, adds 1 to that value. If there isn’t a value inside, the function should return the None value and not attempt to perform any operations.

This function is very easy to write, thanks to match, and will look like Listing 6-5.

  1. fn main() {
  2.     fn plus_one(x: Option<i32>) -> Option<i32> {
  3.         match x {
  4.             None => None,
  5.             Some(i) => Some(i + 1),
  6.         }
  7.     }
  8.     let five = Some(5);
  9.     let six = plus_one(five);
  10.     let none = plus_one(None);
  11. }

Listing 6-5: A function that uses a match expression on an Option<i32>

Let’s examine the first execution of plus_one in more detail. When we call plus_one(five), the variable x in the body of plus_one will have the value Some(5). We then compare that against each match arm:

  1. fn main() {
  2. fn plus_one(x: Option<i32>) -> Option<i32> {
  3. match x {
  4. None => None,
  5. Some(i) => Some(i + 1),
  6. }
  7. }
  8. let five = Some(5);
  9. let six = plus_one(five);
  10. let none = plus_one(None);
  11. }

The Some(5) value doesn’t match the pattern None, so we continue to the next arm:

  1. fn main() {
  2. fn plus_one(x: Option<i32>) -> Option<i32> {
  3. match x {
  4. None => None,
  5. Some(i) => Some(i + 1),
  6. }
  7. }
  8. let five = Some(5);
  9. let six = plus_one(five);
  10. let none = plus_one(None);
  11. }

Does Some(5) match Some(i)? It does! We have the same variant. The i binds to the value contained in Some, so i takes the value 5. The code in the match arm is then executed, so we add 1 to the value of i and create a new Some value with our total 6 inside.

Now let’s consider the second call of plus_one in Listing 6-5, where x is None. We enter the match and compare to the first arm:

  1. fn main() {
  2. fn plus_one(x: Option<i32>) -> Option<i32> {
  3. match x {
  4. None => None,
  5. Some(i) => Some(i + 1),
  6. }
  7. }
  8. let five = Some(5);
  9. let six = plus_one(five);
  10. let none = plus_one(None);
  11. }

It matches! There’s no value to add to, so the program stops and returns the None value on the right side of =>. Because the first arm matched, no other arms are compared.

Combining match and enums is useful in many situations. You’ll see this pattern a lot in Rust code: match against an enum, bind a variable to the data inside, and then execute code based on it. It’s a bit tricky at first, but once you get used to it, you’ll wish you had it in all languages. It’s consistently a user favorite.

Matches Are Exhaustive

There’s one other aspect of match we need to discuss: the arms’ patterns must cover all possibilities. Consider this version of our plus_one function, which has a bug and won’t compile:

  1. fn main() {
  2. fn plus_one(x: Option<i32>) -> Option<i32> {
  3. match x {
  4. Some(i) => Some(i + 1),
  5. }
  6. }
  7. let five = Some(5);
  8. let six = plus_one(five);
  9. let none = plus_one(None);
  10. }

We didn’t handle the None case, so this code will cause a bug. Luckily, it’s a bug Rust knows how to catch. If we try to compile this code, we’ll get this error:

  1. $ cargo run
  2. Compiling enums v0.1.0 (file:///projects/enums)
  3. error[E0004]: non-exhaustive patterns: `None` not covered
  4. --> src/main.rs:3:15
  5. |
  6. 3 | match x {
  7. | ^ pattern `None` not covered
  8. |
  9. note: `Option<i32>` defined here
  10. --> /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:574:1
  11. ::: /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:578:5
  12. |
  13. = note: not covered
  14. = note: the matched value is of type `Option<i32>`
  15. help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  16. |
  17. 4 ~ Some(i) => Some(i + 1),
  18. 5 ~ None => todo!(),
  19. |
  20. For more information about this error, try `rustc --explain E0004`.
  21. error: could not compile `enums` (bin "enums") due to 1 previous error

Rust knows that we didn’t cover every possible case, and even knows which pattern we forgot! Matches in Rust are exhaustive: we must exhaust every last possibility in order for the code to be valid. Especially in the case of Option<T>, when Rust prevents us from forgetting to explicitly handle the None case, it protects us from assuming that we have a value when we might have null, thus making the billion-dollar mistake discussed earlier impossible.

Catch-all Patterns and the _ Placeholder

Using enums, we can also take special actions for a few particular values, but for all other values take one default action. Imagine we’re implementing a game where, if you roll a 3 on a dice roll, your player doesn’t move, but instead gets a new fancy hat. If you roll a 7, your player loses a fancy hat. For all other values, your player moves that number of spaces on the game board. Here’s a match that implements that logic, with the result of the dice roll hardcoded rather than a random value, and all other logic represented by functions without bodies because actually implementing them is out of scope for this example:

  1. fn main() {
  2.     let dice_roll = 9;
  3.     match dice_roll {
  4.         3 => add_fancy_hat(),
  5.         7 => remove_fancy_hat(),
  6.         other => move_player(other),
  7.     }
  8.     fn add_fancy_hat() {}
  9.     fn remove_fancy_hat() {}
  10.     fn move_player(num_spaces: u8) {}
  11. }

For the first two arms, the patterns are the literal values 3 and 7. For the last arm that covers every other possible value, the pattern is the variable we’ve chosen to name other. The code that runs for the other arm uses the variable by passing it to the move_player function.

This code compiles, even though we haven’t listed all the possible values a u8 can have, because the last pattern will match all values not specifically listed. This catch-all pattern meets the requirement that match must be exhaustive. Note that we have to put the catch-all arm last because the patterns are evaluated in order. If we put the catch-all arm earlier, the other arms would never run, so Rust will warn us if we add arms after a catch-all!

Rust also has a pattern we can use when we want a catch-all but don’t want to use the value in the catch-all pattern: _ is a special pattern that matches any value and does not bind to that value. This tells Rust we aren’t going to use the value, so Rust won’t warn us about an unused variable.

Let’s change the rules of the game: now, if you roll anything other than a 3 or a 7, you must roll again. We no longer need to use the catch-all value, so we can change our code to use _ instead of the variable named other:

  1. fn main() {
  2.     let dice_roll = 9;
  3.     match dice_roll {
  4.         3 => add_fancy_hat(),
  5.         7 => remove_fancy_hat(),
  6.         _ => reroll(),
  7.     }
  8.     fn add_fancy_hat() {}
  9.     fn remove_fancy_hat() {}
  10.     fn reroll() {}
  11. }

This example also meets the exhaustiveness requirement because we’re explicitly ignoring all other values in the last arm; we haven’t forgotten anything.

Finally, we’ll change the rules of the game one more time so that nothing else happens on your turn if you roll anything other than a 3 or a 7. We can express that by using the unit value (the empty tuple type we mentioned in “The Tuple Type” section) as the code that goes with the _ arm:

  1. fn main() {
  2.     let dice_roll = 9;
  3.     match dice_roll {
  4.         3 => add_fancy_hat(),
  5.         7 => remove_fancy_hat(),
  6.         _ => (),
  7.     }
  8.     fn add_fancy_hat() {}
  9.     fn remove_fancy_hat() {}
  10. }

Here, we’re telling Rust explicitly that we aren’t going to use any other value that doesn’t match a pattern in an earlier arm, and we don’t want to run any code in this case.

There’s more about patterns and matching that we’ll cover in Chapter 18. For now, we’re going to move on to the if let syntax, which can be useful in situations where the match expression is a bit wordy.