Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Listings in Chapters 06-10 to <Listing> #3973

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
c07a0e7
Converted Listing 6-1 to `<Listing>`
SpectralPixel May 26, 2024
d50cfec
Converted listing 6-2 to `<Listing>`
SpectralPixel May 26, 2024
a5620a2
Converted Listing 6-3 to `<Listing>`
SpectralPixel May 26, 2024
b8f286a
Converted Listing 6-4 to `<Listing>`
SpectralPixel May 26, 2024
2ce82df
Fixed issue with chapter 6 `<Listing>`s
SpectralPixel May 26, 2024
b61957f
Converted Listing 6-5 into `<Listing>`
SpectralPixel May 26, 2024
b3ecb4c
Converted Listing 6-6 to `<Listing>`
SpectralPixel May 26, 2024
1681df1
Converted Listing 7-1 into `<Listing>`
SpectralPixel May 26, 2024
9a9e709
Converted Listing 7-2 into `<Listing>`
SpectralPixel May 26, 2024
f210905
Converted Listing 7-3 to `<Listing>`
SpectralPixel May 26, 2024
36fc4ea
Converted Listing 7-4 to `<Listing>`
SpectralPixel May 26, 2024
550a38d
Converted Listing 7-5 to `<Listing>`
SpectralPixel May 26, 2024
ef7b787
Converted Listing 7-6 to `<Listing>`
SpectralPixel May 26, 2024
19c6ee8
Converted Listing 7-7 to `<Listing>`
SpectralPixel May 26, 2024
89968f4
Converted Listing 7-8 to `<Listing>`
SpectralPixel May 26, 2024
679ce8d
Convert Listing 7-9 to `<Listing>`
SpectralPixel May 26, 2024
9560e81
Converted Listing 7-10 to `<Listing>`
SpectralPixel May 26, 2024
57dd8b3
Converted Listing 7-11 to `<Listing>`
SpectralPixel May 26, 2024
a8da7cd
Converted Listing 7-12 to `<Listing>`
SpectralPixel May 26, 2024
a3e0c74
Convert Listing 7-13 to `<Listing>`
SpectralPixel May 26, 2024
14caac2
Convert Listing 7-14 to `<Listing>`
SpectralPixel May 26, 2024
6a01a1d
Convert Listing 7-15 to `<Listing>`
SpectralPixel May 26, 2024
a99e453
Convert Listing 7-16 to `<Listing>`
SpectralPixel May 26, 2024
ee12949
Convert Listing 7-17 to `<Listing>`
SpectralPixel May 26, 2024
941cbda
Convert unnamed listing to `<Listing>`
SpectralPixel May 26, 2024
61144ad
Converted another unnamed listing to `<Listing>` (chapter 7.4)
SpectralPixel May 26, 2024
526ed69
Convert Listing 7-18 to `<Listing>`
SpectralPixel May 26, 2024
e4fa981
Convert Listing 7-19 to `<Listing>`
SpectralPixel May 26, 2024
5f1ecb6
Convert Listing 7-20 to `<Listing>`
SpectralPixel May 26, 2024
bcd98ea
Convert Listing 7-21 to `<Listing>`
SpectralPixel May 26, 2024
b77cd16
Convert Listing 7-22 to `<Listing>`
SpectralPixel May 26, 2024
06dd489
Converted unnamed listing to `<Listing>` (Chapter 7.5, 1/2)
SpectralPixel May 26, 2024
c2238a0
Converted unnamed listing to `<Listing>` (Chapter 7.5, 2/2)
SpectralPixel May 26, 2024
1ae4070
Convert Listing 8-1 to `<Listing>`
SpectralPixel May 26, 2024
fdf3731
Convert Listing 8-2 to `<Listing>`
SpectralPixel May 26, 2024
0fe78c0
Convert Listing 8-3 to `<Listing>`
SpectralPixel May 26, 2024
05ac182
Convert Listing 8-4 to `<Listing>`
SpectralPixel May 26, 2024
b483b10
Convert Listing 8-5 to `<Listing>`
SpectralPixel May 26, 2024
4dc5b6c
Convert Listing 8-6 to `<Listing>`
SpectralPixel May 26, 2024
7073d33
Convert Listing 8-7 to `<Listing>`
SpectralPixel May 26, 2024
3a8e80f
Convert Listing 8-8 to `<Listing>`
SpectralPixel May 26, 2024
5d1b0fe
Convert Listing 8-9 to `<Listing>`
SpectralPixel May 26, 2024
4054b20
Convert Listing 8-10 to `<Listing>`
SpectralPixel May 26, 2024
4cc112a
Convert Listing 8-11 to `<Listing>`
SpectralPixel May 26, 2024
d95d695
Convert Listing 8-12 to `<Listing>`
SpectralPixel May 26, 2024
11d9027
Convert Listing 8-13 to `<Listing>`
SpectralPixel May 26, 2024
f2a5c4c
Convert Listing 8-14 to `<Listing>`
SpectralPixel May 26, 2024
9d6e07c
Convert Listing 8-15 to `<Listing>`
SpectralPixel May 26, 2024
db528ac
Convert Listing 8-16 to `<Listing>`
SpectralPixel May 26, 2024
6aee816
Convert Listing 8-17 to `<Listing>`
SpectralPixel May 27, 2024
2c4f512
Convert Listing 8-18 to `<Listing>`
SpectralPixel May 27, 2024
93ef988
Convert Listing 8-19 to `<Listing>`
SpectralPixel May 27, 2024
27fcd8b
Convert Listing 8-20 to `<Listing>`
SpectralPixel May 27, 2024
53cc251
Convert Listing 8-21 to `<Listing>`
SpectralPixel May 27, 2024
499b3f8
Convert Listing 8-22 to `<Listing>`
SpectralPixel May 27, 2024
f20f580
Convert Listing 8-23 to `<Listing>`
SpectralPixel May 27, 2024
7029285
Convert Listing 8-24 to `<Listing>`
SpectralPixel May 27, 2024
76131f2
Convert Listing 8-25 to `<Listing>`
SpectralPixel May 27, 2024
41265dd
Convert Listing 9-1 to `<Listing>`
SpectralPixel May 27, 2024
538ac86
Convert Listing 9-2 to `<Listing>`
SpectralPixel May 27, 2024
4891d06
Convert Listing 9-3 to `<Listing>`
SpectralPixel May 27, 2024
fc5bddb
Convert Listing 9-4 to `<Listing>`
SpectralPixel May 27, 2024
073d9fc
Convert Listing 9-5 to `<Listing>`
SpectralPixel May 27, 2024
204d619
Convert Listing 9-6 to `<Listing>`
SpectralPixel May 27, 2024
2e0f94e
Convert Listing 9-7 to `<Listing>`
SpectralPixel May 27, 2024
69cf711
Convert Listing 8-9 to `<Listing>`
SpectralPixel May 27, 2024
b66424e
Convert Listing 9-9 to `<Listing>`
SpectralPixel May 27, 2024
df502ad
Convert Listing 9-10 to `<Listing>`
SpectralPixel May 27, 2024
7287a28
Convert Listing 9-11 to `<Listing>`
SpectralPixel May 27, 2024
29d9280
Convert Listing 9-12 to `<Listing>`
SpectralPixel May 27, 2024
a276eab
Converted Listing 9-13 to `<Listing>` (& more)
SpectralPixel May 27, 2024
897a387
Convert Listing 10-1 to `<Listing>`
SpectralPixel Jun 19, 2024
36451bf
Convert Listing 10-2 to `<Listing>`
SpectralPixel Jun 19, 2024
d43df26
Convert Listing 10-3 to `<Listing>`
SpectralPixel Jun 19, 2024
92324a4
Fixed bugs in `<Listing>`s 10-1 to 10-3 (inclusive)
SpectralPixel Jun 19, 2024
d1c7752
Convert Listing 10-4 to `<Listing>`
SpectralPixel Jun 19, 2024
36be38d
Convert Listing 10-5 to `<Listing>`
SpectralPixel Jun 19, 2024
0a12fff
Convert Listing 10-6 to `<Listing>`
SpectralPixel Jun 19, 2024
9fd5924
Convert Listing 10-7 to `<Listing>`
SpectralPixel Jun 19, 2024
2026278
Convert Listing 10-8 to `<Listing>`
SpectralPixel Jun 19, 2024
a5c7c53
Convert Listing 10-9 to `<Listing>`
SpectralPixel Jun 19, 2024
d021be2
Convert Listing 10-10 to `<Listing>`
SpectralPixel Jun 19, 2024
77b75f7
Convert Listing 10-10 to `<Listing>`
SpectralPixel Jun 19, 2024
83a196d
Convert Listing 10-12 to `<Listing>`
SpectralPixel Jun 19, 2024
42e1379
Convert Listing 10-13 to `<Listing>`
SpectralPixel Jun 19, 2024
b01e396
Convert Listing 10-14 to `<Listing>`
SpectralPixel Jun 19, 2024
8ef7241
Convert Listing 10-15 to `<Listing>`
SpectralPixel Jun 19, 2024
eaee978
Convert Listing 10-16 to `<Listing>`
SpectralPixel Jun 24, 2024
9ebc520
Convert Listing 10-17 to `<Listing>`
SpectralPixel Jun 26, 2024
bb25bd7
Convert Listing 10-18 to `<Listing>`
SpectralPixel Jun 26, 2024
4f640bb
Convert Listing 10-19 to `<Listing>`
SpectralPixel Jun 26, 2024
f272385
Convert Listing 10-20 to `<Listing>`
SpectralPixel Jun 26, 2024
abe0de5
Convert Listing 10-21 to `<Listing>`
SpectralPixel Jun 26, 2024
25d6d28
Convert Listing 10-22 to `<Listing>`
SpectralPixel Jun 26, 2024
a75cc81
Convert Listing 10-23 to `<Listing>`
SpectralPixel Jun 26, 2024
230a955
Convert Listing 10-24 to `<Listing>`
SpectralPixel Jun 26, 2024
bce12b2
Converted unnamed listing to `<Listing>` (Chapter 10.3, 1/2)
SpectralPixel Jun 26, 2024
9c43b1c
Converted unnamed listing to `<Listing>` (Chapter 10.3, 2/2)
SpectralPixel Jun 26, 2024
d1d989d
Convert Listing 10-25 to `<Listing>`
SpectralPixel Jun 26, 2024
27db910
Wrap all `<Listing>`s to comply with the virtual 80 character limit (…
SpectralPixel Jun 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/ch06-01-defining-an-enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ 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.

<Listing number="6-1" caption="Storing the data and `IpAddrKind` variant of an
IP address using a `struct`">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-01/src/main.rs:here}}
```

<span class="caption">Listing 6-1: Storing the data and `IpAddrKind` variant of
an IP address using a `struct`</span>
</Listing>

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
Expand Down Expand Up @@ -140,12 +142,14 @@ 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.

<Listing number="6-2" caption="A `Message` enum whose variants each store
different amounts and types of values">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-02/src/main.rs:here}}
```

<span class="caption">Listing 6-2: A `Message` enum whose variants each store
different amounts and types of values</span>
</Listing>

This enum has four variants with different types:

Expand Down
18 changes: 12 additions & 6 deletions src/ch06-02-match.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ 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.

<Listing number="6-3" caption="An enum and a `match` expression that has the
variants of the enum as its patterns">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-03/src/main.rs:here}}
```

<span class="caption">Listing 6-3: An enum and a `match` expression that has
the variants of the enum as its patterns</span>
</Listing>

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
Expand Down Expand Up @@ -75,12 +77,14 @@ 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.

<Listing number="6-4" caption="A `Coin` enum in which the `Quarter` variant
also holds a `UsState` value">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-04/src/main.rs:here}}
```

<span class="caption">Listing 6-4: A `Coin` enum in which the `Quarter` variant
also holds a `UsState` value</span>
</Listing>

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
Expand Down Expand Up @@ -119,12 +123,14 @@ operations.
This function is very easy to write, thanks to `match`, and will look like
Listing 6-5.

<Listing number="6-5" caption="A function that uses a `match` expression on an
`Option<i32>`">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-05/src/main.rs:here}}
```

<span class="caption">Listing 6-5: A function that uses a `match` expression on
an `Option<i32>`</span>
</Listing>

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
Expand Down
6 changes: 4 additions & 2 deletions src/ch06-03-if-let.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ program in Listing 6-6 that matches on an `Option<u8>` value in the
`config_max` variable but only wants to execute code if the value is the `Some`
variant.

<Listing number="6-6" caption="A `match` that only cares about executing code
when the value is `Some`">

```rust
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs:here}}
```

<span class="caption">Listing 6-6: A `match` that only cares about executing
code when the value is `Some`</span>
</Listing>

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
Expand Down
11 changes: 6 additions & 5 deletions src/ch07-02-defining-modules-to-control-scope-and-privacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ restaurant --lib`. Then enter the code in Listing 7-1 into *src/lib.rs* to
define some modules and function signatures; this code is the front of house
section.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-1" file-name="src/lib.rs" caption="A `front_of_house` module
containing other modules that then contain functions">

```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-01/src/lib.rs}}
```

<span class="caption">Listing 7-1: A `front_of_house` module containing other
modules that then contain functions</span>
</Listing>

We define a module with the `mod` keyword followed by the name of the module
(in this case, `front_of_house`). The body of the module then goes inside curly
Expand All @@ -143,6 +143,8 @@ known as the *module tree*.

Listing 7-2 shows the module tree for the structure in Listing 7-1.

<Listing number="7-2" caption="The module tree for the code in Listing 7-1">

```text
crate
└── front_of_house
Expand All @@ -155,8 +157,7 @@ crate
└── take_payment
```

<span class="caption">Listing 7-2: The module tree for the code in Listing
7-1</span>
</Listing>

This tree shows how some of the modules nest inside other modules; for example,
`hosting` nests inside `front_of_house`. The tree also shows that some modules
Expand Down
50 changes: 27 additions & 23 deletions src/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ The `eat_at_restaurant` function is part of our library crate’s public API, so
we mark it with the `pub` keyword. In the [“Exposing Paths with the `pub`
Keyword”][pub]<!-- ignore --> section, we’ll go into more detail about `pub`.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-3" file-name="src/lib.rs" caption="Calling the
`add_to_waitlist` function using absolute and relative paths">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-03/src/lib.rs}}
```

<span class="caption">Listing 7-3: Calling the `add_to_waitlist` function using
absolute and relative paths</span>
</Listing>

The first time we call the `add_to_waitlist` function in `eat_at_restaurant`,
we use an absolute path. The `add_to_waitlist` function is defined in the same
Expand Down Expand Up @@ -70,12 +70,14 @@ each other.
Let’s try to compile Listing 7-3 and find out why it won’t compile yet! The
errors we get are shown in Listing 7-4.

<Listing number="7-4" caption="Compiler errors from building the code in
Listing 7-3">

```console
{{#include ../listings/ch07-managing-growing-projects/listing-07-03/output.txt}}
```

<span class="caption">Listing 7-4: Compiler errors from building the code in
Listing 7-3</span>
</Listing>

The error messages say that module `hosting` is private. In other words, we
have the correct paths for the `hosting` module and the `add_to_waitlist`
Expand Down Expand Up @@ -105,24 +107,26 @@ private. We want the `eat_at_restaurant` function in the parent module to have
access to the `add_to_waitlist` function in the child module, so we mark the
`hosting` module with the `pub` keyword, as shown in Listing 7-5.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-5" file-name="src/lib.rs" caption="Declaring the `hosting`
module as `pub` to use it from `eat_at_restaurant`">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-05/src/lib.rs}}
```

<span class="caption">Listing 7-5: Declaring the `hosting` module as `pub` to
use it from `eat_at_restaurant`</span>
</Listing>

Unfortunately, the code in Listing 7-5 still results in compiler errors, as
shown in Listing 7-6.

<Listing number="7-6" caption="Compiler errors from building the code in
Listing 7-5">

```console
{{#include ../listings/ch07-managing-growing-projects/listing-07-05/output.txt}}
```

<span class="caption">Listing 7-6: Compiler errors from building the code in
Listing 7-5</span>
</Listing>

What happened? Adding the `pub` keyword in front of `mod hosting` makes the
module public. With this change, if we can access `front_of_house`, we can
Expand All @@ -140,15 +144,15 @@ modules.
Let’s also make the `add_to_waitlist` function public by adding the `pub`
keyword before its definition, as in Listing 7-7.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-7" file-name="src/lib.rs" caption="Adding the `pub` keyword
to `mod hosting` and `fn add_to_waitlist` lets us call the function from
`eat_at_restaurant`">

```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-07/src/lib.rs}}
```

<span class="caption">Listing 7-7: Adding the `pub` keyword to `mod hosting`
and `fn add_to_waitlist` lets us call the function from
`eat_at_restaurant`</span>
</Listing>

Now the code will compile! To see why adding the `pub` keyword lets us use
these paths in `eat_at_restaurant` with respect to the privacy rules, let’s look
Expand Down Expand Up @@ -216,14 +220,14 @@ function `fix_incorrect_order` defined in the `back_of_house` module calls the
function `deliver_order` defined in the parent module by specifying the path to
`deliver_order`, starting with `super`.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-8" file-name="src/lib.rs" caption="Calling a function using
a relative path starting with `super`">

```rust,noplayground,test_harness
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-08/src/lib.rs}}
```

<span class="caption">Listing 7-8: Calling a function using a relative path
starting with `super`</span>
</Listing>

The `fix_incorrect_order` function is in the `back_of_house` module, so we can
use `super` to go to the parent module of `back_of_house`, which in this case
Expand All @@ -247,14 +251,14 @@ comes with a meal, but the chef decides which fruit accompanies the meal based
on what’s in season and in stock. The available fruit changes quickly, so
customers can’t choose the fruit or even see which fruit they’ll get.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-9" file-name="src/lib.rs" caption="A struct with some public
fields and some private fields">

```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-09/src/lib.rs}}
```

<span class="caption">Listing 7-9: A struct with some public fields and some
private fields</span>
</Listing>

Because the `toast` field in the `back_of_house::Breakfast` struct is public,
in `eat_at_restaurant` we can write and read to the `toast` field using dot
Expand All @@ -272,14 +276,14 @@ have such a function, we couldn’t create an instance of `Breakfast` in
In contrast, if we make an enum public, all of its variants are then public. We
only need the `pub` before the `enum` keyword, as shown in Listing 7-10.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-10" file-name="src/lib.rs" caption="Designating an enum as
public makes all its variants public">

```rust,noplayground
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-10/src/lib.rs}}
```

<span class="caption">Listing 7-10: Designating an enum as public makes all its
variants public</span>
</Listing>

Because we made the `Appetizer` enum public, we can use the `Soup` and `Salad`
variants in `eat_at_restaurant`.
Expand Down
Loading