3 Often, a simple `if`/`else` isn't enough, because you have more than two
4 possible options. Also, `else` conditions can get incredibly complicated, so
7 Rust has a keyword, `match`, that allows you to replace complicated `if`/`else`
8 groupings with something more powerful. Check it out:
16 3 => println!("three"),
17 4 => println!("four"),
18 5 => println!("five"),
19 _ => println!("something else"),
23 `match` takes an expression and then branches based on its value. Each *arm* of
24 the branch is of the form `val => expression`. When the value matches, that arm's
25 expression will be evaluated. It's called `match` because of the term 'pattern
26 matching', which `match` is an implementation of.
28 So what's the big advantage here? Well, there are a few. First of all, `match`
29 enforces *exhaustiveness checking*. Do you see that last arm, the one with the
30 underscore (`_`)? If we remove that arm, Rust will give us an error:
33 error: non-exhaustive patterns: `_` not covered
36 In other words, Rust is trying to tell us we forgot a value. Because `x` is an
37 integer, Rust knows that it can have a number of different values – for example,
38 `6`. Without the `_`, however, there is no arm that could match, and so Rust refuses
39 to compile. `_` acts like a *catch-all arm*. If none of the other arms match,
40 the arm with `_` will, and since we have this catch-all arm, we now have an arm
41 for every possible value of `x`, and so our program will compile successfully.
43 `match` statements also destructure enums, as well. Remember this code from the
47 use std::cmp::Ordering;
49 fn cmp(a: i32, b: i32) -> Ordering {
50 if a < b { Ordering::Less }
51 else if a > b { Ordering::Greater }
52 else { Ordering::Equal }
59 let ordering = cmp(x, y);
61 if ordering == Ordering::Less {
63 } else if ordering == Ordering::Greater {
65 } else if ordering == Ordering::Equal {
71 We can re-write this as a `match`:
74 use std::cmp::Ordering;
76 fn cmp(a: i32, b: i32) -> Ordering {
77 if a < b { Ordering::Less }
78 else if a > b { Ordering::Greater }
79 else { Ordering::Equal }
87 Ordering::Less => println!("less"),
88 Ordering::Greater => println!("greater"),
89 Ordering::Equal => println!("equal"),
94 This version has way less noise, and it also checks exhaustively to make sure
95 that we have covered all possible variants of `Ordering`. With our `if`/`else`
96 version, if we had forgotten the `Greater` case, for example, our program would
97 have happily compiled. If we forget in the `match`, it will not. Rust helps us
98 make sure to cover all of our bases.
100 `match` expressions also allow us to get the values contained in an `enum`
101 (also known as destructuring) as follows:
110 let x = OptionalInt::Value(5);
111 let y = OptionalInt::Missing;
114 OptionalInt::Value(n) => println!("x is {}", n),
115 OptionalInt::Missing => println!("x is missing!"),
119 OptionalInt::Value(n) => println!("y is {}", n),
120 OptionalInt::Missing => println!("y is missing!"),
125 That is how you can get and use the values contained in `enum`s.
126 It can also allow us to handle errors or unexpected computations; for example, a
127 function that is not guaranteed to be able to compute a result (an `i32` here)
128 could return an `OptionalInt`, and we would handle that value with a `match`.
129 As you can see, `enum` and `match` used together are quite useful!
131 `match` is also an expression, which means we can use it on the right-hand
132 side of a `let` binding or directly where an expression is used. We could
133 also implement the previous example like this:
136 use std::cmp::Ordering;
138 fn cmp(a: i32, b: i32) -> Ordering {
139 if a < b { Ordering::Less }
140 else if a > b { Ordering::Greater }
141 else { Ordering::Equal }
148 println!("{}", match cmp(x, y) {
149 Ordering::Less => "less",
150 Ordering::Greater => "greater",
151 Ordering::Equal => "equal",
156 Sometimes, it's a nice pattern.