You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rewrite paragraph in 'match' to be more concise and readable. Start
correcting use of ':' in sentences.
The colon `:` should be used only when the sentence preceeding it is a
complete sentence. If this is not the case, then a `;` should be used;
this denotes that the following fragment is a part of the previous
fragment.
Copy file name to clipboardExpand all lines: src/doc/book/match.md
+5-13Lines changed: 5 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -23,26 +23,18 @@ match x {
23
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
-
matching’, which `match` is an implementation of. There’s an [entire section on
26
+
matching’, which `match` is an implementation of. There’s a [separate section on
27
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? Well, there are a few. First of all, `match`
32
-
enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the
33
-
underscore (`_`)? If we remove that arm, Rust will give us an error:
31
+
One of the many advantages of `match` is it enforces ‘exhaustiveness checking’. For example if we remove the last arm with the underscore `_`, the compiler will give us an error:
34
32
35
33
```text
36
34
error: non-exhaustive patterns: `_` not covered
37
35
```
38
36
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
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.
37
+
Rust is telling us that we forgot a value. The compiler infers from `x` that it can have any positive 32bit value; for example 1 to 2,147,483,647. The `_` acts as a 'catch-all', and will catch all possible values that *aren't* specified in an arm of `match`. As you can see with the previous example, we provide `match` arms for integers 1-5, if `x` is 6 or any other value, then it is caught by `_`.
46
38
47
39
`match` is also an expression, which means we can use it on the right-hand
48
40
side of a `let` binding or directly where an expression is used:
@@ -60,7 +52,7 @@ let number = match x {
60
52
};
61
53
```
62
54
63
-
Sometimes it’s a nice way of converting something from one type to another.
55
+
Sometimes it’s a nice way of converting something from one type to another; in this example the integers are converted to `String`.
0 commit comments