Skip to content

Commit 836c8a8

Browse files
committed
TRPL editing: match
1 parent 2137088 commit 836c8a8

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/doc/trpl/match.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
% Match
22

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`
86
groupings with something more powerful. Check it out:
97

108
```rust
@@ -20,28 +18,31 @@ match x {
2018
}
2119
```
2220

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
2424
the branch is of the form `val => expression`. When the value matches, that arm’s
2525
expression will be evaluated. It’s called `match` because of the term ‘pattern
2626
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.
2828

2929
[patterns]: patterns.html
3030

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
3333
underscore (`_`)? If we remove that arm, Rust will give us an error:
3434

3535
```text
3636
error: non-exhaustive patterns: `_` not covered
3737
```
3838

3939
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.
4546

4647
`match` is also an expression, which means we can use it on the right-hand
4748
side of a `let` binding or directly where an expression is used:
@@ -59,4 +60,4 @@ let numer = match x {
5960
};
6061
```
6162

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

Comments
 (0)