1
1
% Match
2
2
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
5
- what’s the solution?
6
-
7
- Rust has a keyword, ` match ` , that allows you to replace complicated ` if ` /` else `
3
+ Often, a simple [ ` if ` ] [ if ] /` else ` isn’t enough, because you have more than two
4
+ possible options. Also, conditions can get quite complex. Rust
5
+ has a keyword, ` match ` , that allows you to replace complicated ` if ` /` else `
8
6
groupings with something more powerful. Check it out:
9
7
10
8
``` rust
@@ -20,28 +18,31 @@ match x {
20
18
}
21
19
```
22
20
23
- ` match ` takes an expression and then branches based on its value. Each * arm* of
21
+ [ if ] : if.html
22
+
23
+ ` match ` takes an expression and then branches based on its value. Each ‘arm’ of
24
24
the branch is of the form ` val => expression ` . When the value matches, that arm’s
25
25
expression will be evaluated. It’s called ` match ` because of the term ‘pattern
26
26
matching’, which ` match ` is an implementation of. There’s an [ entire section on
27
- patterns] [ patterns ] coming up next, that covers all the options that fit here.
27
+ patterns] [ patterns ] that covers all the patterns that are possible here.
28
28
29
29
[ patterns ] : patterns.html
30
30
31
- So what’s the big advantage here ? Well, there are a few. First of all, ` match `
32
- enforces * exhaustiveness checking* . Do you see that last arm, the one with the
31
+ So what’s the big advantage? Well, there are a few. First of all, ` match `
32
+ enforces ‘ exhaustiveness checking’ . Do you see that last arm, the one with the
33
33
underscore (` _ ` )? If we remove that arm, Rust will give us an error:
34
34
35
35
``` text
36
36
error: non-exhaustive patterns: `_` not covered
37
37
```
38
38
39
39
In other words, Rust is trying to tell us we forgot a value. Because ` x ` is an
40
- integer, Rust knows that it can have a number of different values – for example,
41
- ` 6 ` . Without the ` _ ` , however, there is no arm that could match, and so Rust refuses
42
- to compile. ` _ ` acts like a ‘catch-all arm’. If none of the other arms match,
43
- the arm with ` _ ` will, and since we have this catch-all arm, we now have an arm
44
- for every possible value of ` x ` , and so our program will compile successfully.
40
+ integer, Rust knows that it can have a number of different values – for
41
+ example, ` 6 ` . Without the ` _ ` , however, there is no arm that could match, and
42
+ so Rust refuses to compile the code. ` _ ` acts like a ‘catch-all arm’. If none
43
+ of the other arms match, the arm with ` _ ` will, and since we have this
44
+ catch-all arm, we now have an arm for every possible value of ` x ` , and so our
45
+ program will compile successfully.
45
46
46
47
` match ` is also an expression, which means we can use it on the right-hand
47
48
side of a ` let ` binding or directly where an expression is used:
@@ -59,4 +60,4 @@ let numer = match x {
59
60
};
60
61
```
61
62
62
- Sometimes, it’s a nice way of converting things .
63
+ Sometimes it’s a nice way of converting something from one type to another .
0 commit comments