TRPL editing: match
This commit is contained in:
parent
213708867e
commit
836c8a826b
1 changed files with 16 additions and 15 deletions
|
@ -1,10 +1,8 @@
|
||||||
% Match
|
% Match
|
||||||
|
|
||||||
Often, a simple `if`/`else` isn’t enough, because you have more than two
|
Often, a simple [`if`][if]/`else` isn’t enough, because you have more than two
|
||||||
possible options. Also, `else` conditions can get incredibly complicated, so
|
possible options. Also, conditions can get quite complex. Rust
|
||||||
what’s the solution?
|
has a keyword, `match`, that allows you to replace complicated `if`/`else`
|
||||||
|
|
||||||
Rust has a keyword, `match`, that allows you to replace complicated `if`/`else`
|
|
||||||
groupings with something more powerful. Check it out:
|
groupings with something more powerful. Check it out:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
@ -20,16 +18,18 @@ match x {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`match` takes an expression and then branches based on its value. Each *arm* of
|
[if]: if.html
|
||||||
|
|
||||||
|
`match` takes an expression and then branches based on its value. Each ‘arm’ of
|
||||||
the branch is of the form `val => expression`. When the value matches, that arm’s
|
the branch is of the form `val => expression`. When the value matches, that arm’s
|
||||||
expression will be evaluated. It’s called `match` because of the term ‘pattern
|
expression will be evaluated. It’s called `match` because of the term ‘pattern
|
||||||
matching’, which `match` is an implementation of. There’s an [entire section on
|
matching’, which `match` is an implementation of. There’s an [entire section on
|
||||||
patterns][patterns] coming up next, that covers all the options that fit here.
|
patterns][patterns] that covers all the patterns that are possible here.
|
||||||
|
|
||||||
[patterns]: patterns.html
|
[patterns]: patterns.html
|
||||||
|
|
||||||
So what’s the big advantage here? Well, there are a few. First of all, `match`
|
So what’s the big advantage? Well, there are a few. First of all, `match`
|
||||||
enforces *exhaustiveness checking*. Do you see that last arm, the one with the
|
enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the
|
||||||
underscore (`_`)? If we remove that arm, Rust will give us an error:
|
underscore (`_`)? If we remove that arm, Rust will give us an error:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
|
@ -37,11 +37,12 @@ error: non-exhaustive patterns: `_` not covered
|
||||||
```
|
```
|
||||||
|
|
||||||
In other words, Rust is trying to tell us we forgot a value. Because `x` is an
|
In other words, Rust is trying to tell us we forgot a value. Because `x` is an
|
||||||
integer, Rust knows that it can have a number of different values – for example,
|
integer, Rust knows that it can have a number of different values – for
|
||||||
`6`. Without the `_`, however, there is no arm that could match, and so Rust refuses
|
example, `6`. Without the `_`, however, there is no arm that could match, and
|
||||||
to compile. `_` acts like a ‘catch-all arm’. If none of the other arms match,
|
so Rust refuses to compile the code. `_` acts like a ‘catch-all arm’. If none
|
||||||
the arm with `_` will, and since we have this catch-all arm, we now have an arm
|
of the other arms match, the arm with `_` will, and since we have this
|
||||||
for every possible value of `x`, and so our program will compile successfully.
|
catch-all arm, we now have an arm for every possible value of `x`, and so our
|
||||||
|
program will compile successfully.
|
||||||
|
|
||||||
`match` is also an expression, which means we can use it on the right-hand
|
`match` is also an expression, which means we can use it on the right-hand
|
||||||
side of a `let` binding or directly where an expression is used:
|
side of a `let` binding or directly where an expression is used:
|
||||||
|
@ -59,4 +60,4 @@ let numer = match x {
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Sometimes, it’s a nice way of converting things.
|
Sometimes it’s a nice way of converting something from one type to another.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue