diff --git a/src/ch06-01-defining-an-enum.md b/src/ch06-01-defining-an-enum.md index eacd091bd4..3462f38917 100644 --- a/src/ch06-01-defining-an-enum.md +++ b/src/ch06-01-defining-an-enum.md @@ -59,12 +59,13 @@ only know what *kind* it is. Given that you just learned about structs in Chapter 5, you might be tempted to tackle this problem with structs as shown in Listing 6-1. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs:here}} ``` -Listing 6-1: Storing the data and `IpAddrKind` variant of -an IP address using a `struct` + Here, we’ve defined a struct `IpAddr` that has two fields: a `kind` field that is of type `IpAddrKind` (the enum we defined previously) and an `address` field @@ -140,12 +141,13 @@ more about bringing types into scope in Chapter 7. Let’s look at another example of an enum in Listing 6-2: this one has a wide variety of types embedded in its variants. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs:here}} ``` -Listing 6-2: A `Message` enum whose variants each store -different amounts and types of values + This enum has four variants with different types: diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md index 6a510df402..2bb8929798 100644 --- a/src/ch06-02-match.md +++ b/src/ch06-02-match.md @@ -22,12 +22,13 @@ function that takes an unknown US coin and, in a similar way as the counting machine, determines which coin it is and returns its value in cents, as shown in Listing 6-3. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs:here}} ``` -Listing 6-3: An enum and a `match` expression that has -the variants of the enum as its patterns + Let’s break down the `match` in the `value_in_cents` function. First we list 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 our `enum` by changing the `Quarter` variant to include a `UsState` value stored inside it, which we’ve done in Listing 6-4. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs:here}} ``` -Listing 6-4: A `Coin` enum in which the `Quarter` variant -also holds a `UsState` value + Let’s imagine that a friend is trying to collect all 50 state quarters. While we sort our loose change by coin type, we’ll also call out the name of the @@ -119,12 +121,13 @@ operations. This function is very easy to write, thanks to `match`, and will look like Listing 6-5. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:here}} ``` -Listing 6-5: A function that uses a `match` expression on -an `Option` + Let’s examine the first execution of `plus_one` in more detail. When we call `plus_one(five)`, the variable `x` in the body of `plus_one` will have the diff --git a/src/ch06-03-if-let.md b/src/ch06-03-if-let.md index c9bfbf3c7f..fee6b2caf3 100644 --- a/src/ch06-03-if-let.md +++ b/src/ch06-03-if-let.md @@ -6,12 +6,13 @@ program in Listing 6-6 that matches on an `Option` value in the `config_max` variable but only wants to execute code if the value is the `Some` variant. ++ ```rust {{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs:here}} ``` -Listing 6-6: A `match` that only cares about executing -code when the value is `Some` + If the value is `Some`, we print out the value in the `Some` variant by binding the value to the variable `max` in the pattern. We don’t want to do anything