@@ -22,12 +22,13 @@ function that takes an unknown US coin and, in a similar way as the counting
22
22
machine, determines which coin it is and returns its value in cents, as shown
23
23
in Listing 6-3.
24
24
25
+ <Listing number =" 6-3 " caption =" An enum and a `match` expression that has the variants of the enum as its patterns " >
26
+
25
27
``` rust
26
28
{{#rustdoc_include .. / listings / ch06 - enums - and - pattern - matching / listing - 06 - 03 / src / main . rs: here }}
27
29
```
28
30
29
- <span class =" caption " >Listing 6-3: An enum and a ` match ` expression that has
30
- the variants of the enum as its patterns</span >
31
+ </Listing >
31
32
32
33
Let’s break down the ` match ` in the ` value_in_cents ` function. First we list
33
34
the ` match ` keyword followed by an expression, which in this case is the value
@@ -75,12 +76,13 @@ designs, so only quarters have this extra value. We can add this information to
75
76
our ` enum ` by changing the ` Quarter ` variant to include a ` UsState ` value
76
77
stored inside it, which we’ve done in Listing 6-4.
77
78
79
+ <Listing number =" 6-4 " caption =" A `Coin` enum in which the `Quarter` variant also holds a `UsState` value " >
80
+
78
81
``` rust
79
82
{{#rustdoc_include .. / listings / ch06 - enums - and - pattern - matching / listing - 06 - 04 / src / main . rs: here }}
80
83
```
81
84
82
- <span class =" caption " >Listing 6-4: A ` Coin ` enum in which the ` Quarter ` variant
83
- also holds a ` UsState ` value</span >
85
+ </Listing >
84
86
85
87
Let’s imagine that a friend is trying to collect all 50 state quarters. While
86
88
we sort our loose change by coin type, we’ll also call out the name of the
@@ -119,12 +121,13 @@ operations.
119
121
This function is very easy to write, thanks to ` match ` , and will look like
120
122
Listing 6-5.
121
123
124
+ <Listing number =" 6-5 " caption =" A function that uses a `match` expression on an `Option<i32>` " >
125
+
122
126
``` rust
123
127
{{#rustdoc_include .. / listings / ch06 - enums - and - pattern - matching / listing - 06 - 05 / src / main . rs: here }}
124
128
```
125
129
126
- <span class =" caption " >Listing 6-5: A function that uses a ` match ` expression on
127
- an ` Option<i32> ` </span >
130
+ </Listing >
128
131
129
132
Let’s examine the first execution of ` plus_one ` in more detail. When we call
130
133
` plus_one(five) ` , the variable ` x ` in the body of ` plus_one ` will have the
0 commit comments