Skip to content

Commit c76f1b4

Browse files
committed
Propagate tech review edits to ch06 src
1 parent 5f7f99c commit c76f1b4

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
22
// ANCHOR: here
33
let some_number = Some(5);
4-
let some_string = Some("a string");
4+
let some_char = Some('e');
55

66
let absent_number: Option<i32> = None;
77
// ANCHOR_END: here

src/ch06-00-enums.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@ how pattern matching in the `match` expression makes it easy to run different
99
code for different values of an enum. Finally, we’ll cover how the `if let`
1010
construct is another convenient and concise idiom available to handle enums in
1111
your code.
12-
13-
Enums are a feature in many languages, but their capabilities differ in each
14-
language. Rust’s enums are most similar to *algebraic data types* in functional
15-
languages, such as F#, OCaml, and Haskell.

src/ch06-01-defining-an-enum.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
## Defining an Enum
22

3-
Enums are a way of defining custom data types in a different way than you do
4-
with structs. Let’s look at a situation we might want to express in code and
5-
see why enums are useful and more appropriate than structs in this case. Say we
6-
need to work with IP addresses. Currently, two major standards are used for IP
7-
addresses: version four and version six. Because these are the only
8-
possibilities for an IP address that our program will come across, we can
9-
*enumerate* all possible variants, which is where enumeration gets its name.
3+
Where structs give you a way of grouping together related fields and data, like
4+
a `Rectangle` with its `width` and `height`, enums give you a way of saying a
5+
value is one of a possible set of values. For example, we may want to say that
6+
`Rectangle` is one of a set of possible shapes that also includes `Circle` and
7+
`Triangle`. To do this, Rust allows us to encode these possibilities as an enum.
8+
9+
Let’s look at a situation we might want to express in code and see why enums
10+
are useful and more appropriate than structs in this case. Say we need to work
11+
with IP addresses. Currently, two major standards are used for IP addresses:
12+
version four and version six. Because these are the only possibilities for an
13+
IP address that our program will come across, we can *enumerate* all possible
14+
variants, which is where enumeration gets its name.
1015

1116
Any IP address can be either a version four or a version six address, but not
1217
both at the same time. That property of IP addresses makes the enum data
@@ -183,12 +188,14 @@ useful: `Option`.
183188

184189
This section explores a case study of `Option`, which is another enum defined
185190
by the standard library. The `Option` type encodes the very common scenario in
186-
which a value could be something or it could be nothing. For example, if you
187-
request the first of a list containing items, you would get a value. If you
188-
request the first item of an empty list, you would get nothing. Expressing this
189-
concept in terms of the type system means the compiler can check whether you’ve
190-
handled all the cases you should be handling; this functionality can prevent
191-
bugs that are extremely common in other programming languages.
191+
which a value could be something or it could be nothing.
192+
193+
For example, if you request the first of a list containing items, you would get
194+
a value. If you request the first item of an empty list, you would get nothing.
195+
Expressing this concept in terms of the type system means the compiler can
196+
check whether you’ve handled all the cases you should be handling; this
197+
functionality can prevent bugs that are extremely common in other programming
198+
languages.
192199

193200
Programming language design is often thought of in terms of which features you
194201
include, but the features you exclude are important too. Rust doesn’t have the
@@ -246,8 +253,8 @@ types and string types:
246253
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/no-listing-06-option-examples/src/main.rs:here}}
247254
```
248255

249-
The type of `some_number` is `Option<i32>`. The type of `some_string` is
250-
`Option<&str>`, which is a different type. Rust can infer these types because
256+
The type of `some_number` is `Option<i32>`. The type of `some_char` is
257+
`Option<char>`, which is a different type. Rust can infer these types because
251258
we’ve specified a value inside the `Some` variant. For `absent_number`, Rust
252259
requires us to annotate the overall `Option` type: the compiler can’t infer the
253260
type that the corresponding `Some` variant will hold by looking only at a

src/ch06-02-match.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ down a track with variously sized holes along it, and each coin falls through
1414
the first hole it encounters that it fits into. In the same way, values go
1515
through each pattern in a `match`, and at the first pattern the value “fits,”
1616
the value falls into the associated code block to be used during execution.
17+
1718
Speaking of coins, let’s use them as an example using `match`! We can write a
1819
function that takes an unknown United States coin and, in a similar way as the
1920
counting machine, determines which coin it is and return its value in cents, as
@@ -50,9 +51,10 @@ entire `match` expression.
5051

5152
We don’t typically use curly brackets if the match arm code is short, as it is
5253
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`:
5658

5759
```rust
5860
{{#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.
161163

162164
### Matches Are Exhaustive
163165

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:
166169

167170
```rust,ignore,does_not_compile
168171
{{#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
208211
`u8` can have, because the last pattern will match all values not specifically
209212
listed. This catch-all pattern meets the requirement that `match` must be
210213
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!
213216

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.
218221

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`:
222225

223226
```rust
224227
{{#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`:
227230
This example also meets the exhaustiveness requirement because we’re explicitly
228231
ignoring all other values in the last arm; we haven’t forgotten anything.
229232

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
233236
Type”][tuples]<!-- ignore --> section) as the code that goes with the `_` arm:
234237

235238
```rust

0 commit comments

Comments
 (0)