@@ -14,6 +14,7 @@ down a track with variously sized holes along it, and each coin falls through
14
14
the first hole it encounters that it fits into. In the same way, values go
15
15
through each pattern in a ` match ` , and at the first pattern the value “fits,”
16
16
the value falls into the associated code block to be used during execution.
17
+
17
18
Speaking of coins, let’s use them as an example using ` match ` ! We can write a
18
19
function that takes an unknown United States coin and, in a similar way as the
19
20
counting machine, determines which coin it is and return its value in cents, as
@@ -50,9 +51,10 @@ entire `match` expression.
50
51
51
52
We don’t typically use curly brackets if the match arm code is short, as it is
52
53
in Listing 6-3 where each arm just returns a value. If you want to run multiple
53
- lines of code in a match arm, you must use curly brackets. For example, the
54
- following code prints “Lucky penny!” every time the method is called with a
55
- ` Coin::Penny ` , but still returns the last value of the block, ` 1 ` :
54
+ lines of code in a match arm, you must use curly brackets, and the comma
55
+ following the arm is then optional. For example, the following code prints
56
+ “Lucky penny!” every time the method is called with a ` Coin::Penny ` , but still
57
+ returns the last value of the block, ` 1 ` :
56
58
57
59
``` rust
58
60
{{#rustdoc_include .. / listings / ch06 - enums - and - pattern - matching / no - listing - 08 - match - arm - multiple - lines / src / main . rs: here }}
@@ -161,8 +163,9 @@ consistently a user favorite.
161
163
162
164
### Matches Are Exhaustive
163
165
164
- There’s one other aspect of ` match ` we need to discuss. Consider this version
165
- of our ` plus_one ` function that has a bug and won’t compile:
166
+ There’s one other aspect of ` match ` we need to discuss: the arms’ patterns must
167
+ cover all possibilities. Consider this version of our ` plus_one ` function,
168
+ which has a bug and won’t compile:
166
169
167
170
``` rust,ignore,does_not_compile
168
171
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-10-non-exhaustive-match/src/main.rs:here}}
@@ -208,17 +211,17 @@ This code compiles, even though we haven’t listed all the possible values a
208
211
` u8 ` can have, because the last pattern will match all values not specifically
209
212
listed. This catch-all pattern meets the requirement that ` match ` must be
210
213
exhaustive. Note that we have to put the catch-all arm last because the
211
- patterns are evaluated in order. Rust will warn us if we add arms after a
212
- catch-all because those later arms would never match !
214
+ patterns are evaluated in order. If we put the catch-all arm earlier, the other
215
+ arms would never run, so Rust will warn us if we add arms after a catch-all !
213
216
214
- Rust also has a pattern we can use when we don’t want to use the value in the
215
- catch-all pattern: ` _ ` , which is a special pattern that matches any value and
216
- does not bind to that value. This tells Rust we aren’t going to use the value,
217
- so Rust won’t warn us about an unused variable.
217
+ Rust also has a pattern we can use when we want a catch-all but don’t want to
218
+ * use * the value in the catch-all pattern: ` _ ` is a special pattern that matches
219
+ any value and does not bind to that value. This tells Rust we aren’t going to
220
+ use the value, so Rust won’t warn us about an unused variable.
218
221
219
- Let’s change the rules of the game to be that if you roll anything other than
220
- a 3 or a 7, you must roll again. We don’t need to use the value in that case,
221
- so we can change our code to use ` _ ` instead of the variable named ` other ` :
222
+ Let’s change the rules of the game: now, if you roll anything other than a 3 or
223
+ a 7, you must roll again. We no longer need to use the catch-all value, so we
224
+ can change our code to use ` _ ` instead of the variable named ` other ` :
222
225
223
226
``` rust
224
227
{{#rustdoc_include .. / listings / ch06 - enums - and - pattern - matching / no - listing - 16 - underscore - catchall / src / main . rs: here }}
@@ -227,9 +230,9 @@ so we can change our code to use `_` instead of the variable named `other`:
227
230
This example also meets the exhaustiveness requirement because we’re explicitly
228
231
ignoring all other values in the last arm; we haven’t forgotten anything.
229
232
230
- If we change the rules of the game one more time, so that nothing else happens
231
- on your turn if you roll anything other than a 3 or a 7, we can express that
232
- by using the unit value (the empty tuple type we mentioned in [ “The Tuple
233
+ Finally, we’ll change the rules of the game one more time, so that nothing else
234
+ happens on your turn if you roll anything other than a 3 or a 7. We can express
235
+ that by using the unit value (the empty tuple type we mentioned in [ “The Tuple
233
236
Type”] [ tuples ] <!-- ignore --> section) as the code that goes with the ` _ ` arm:
234
237
235
238
``` rust
0 commit comments