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 Chapter 07 to <Listing> #3979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7cd0748
Converted Listing 7-1 into `<Listing>`
SpectralPixel May 26, 2024
3d32026
Converted Listing 7-2 into `<Listing>`
SpectralPixel May 26, 2024
12c4a76
Converted Listing 7-3 to `<Listing>`
SpectralPixel May 26, 2024
4513d38
Converted Listing 7-4 to `<Listing>`
SpectralPixel May 26, 2024
f79de25
Converted Listing 7-5 to `<Listing>`
SpectralPixel May 26, 2024
1cd2240
Converted Listing 7-6 to `<Listing>`
SpectralPixel May 26, 2024
e297235
Converted Listing 7-7 to `<Listing>`
SpectralPixel May 26, 2024
8f5e623
Converted Listing 7-8 to `<Listing>`
SpectralPixel May 26, 2024
2a52d9c
Convert Listing 7-9 to `<Listing>`
SpectralPixel May 26, 2024
cc71c02
Converted Listing 7-10 to `<Listing>`
SpectralPixel May 26, 2024
db0262c
Converted Listing 7-11 to `<Listing>`
SpectralPixel May 26, 2024
c1ba675
Converted Listing 7-12 to `<Listing>`
SpectralPixel May 26, 2024
6178940
Convert Listing 7-13 to `<Listing>`
SpectralPixel May 26, 2024
9996cae
Convert Listing 7-14 to `<Listing>`
SpectralPixel May 26, 2024
4f86040
Convert Listing 7-15 to `<Listing>`
SpectralPixel May 26, 2024
744d4df
Convert Listing 7-16 to `<Listing>`
SpectralPixel May 26, 2024
cd5a840
Convert Listing 7-17 to `<Listing>`
SpectralPixel May 26, 2024
be0f5f9
Convert unnamed listing to `<Listing>`
SpectralPixel May 26, 2024
fbaee3d
Converted another unnamed listing to `<Listing>` (chapter 7.4)
SpectralPixel May 26, 2024
de599ba
Convert Listing 7-18 to `<Listing>`
SpectralPixel May 26, 2024
68a4499
Convert Listing 7-19 to `<Listing>`
SpectralPixel May 26, 2024
3ae0b41
Convert Listing 7-20 to `<Listing>`
SpectralPixel May 26, 2024
2c1ebb1
Convert Listing 7-21 to `<Listing>`
SpectralPixel May 26, 2024
f294ad7
Convert Listing 7-22 to `<Listing>`
SpectralPixel May 26, 2024
50ad3cf
Converted unnamed listing to `<Listing>` (Chapter 7.5, 1/2)
SpectralPixel May 26, 2024
1d1b40c
Converted unnamed listing to `<Listing>` (Chapter 7.5, 2/2)
SpectralPixel May 26, 2024
c521d2d
Chapter 7 - Wrap all `<Listing>`s to comply with the virtual 80 chara…
SpectralPixel Jul 17, 2024
cfd25e7
Convert unnamed `<Listing>` (Chapter 7.2)
SpectralPixel Jul 17, 2024
b69404e
Convert unnamed `<Listing>` (Chapter 7.2)
SpectralPixel Jul 17, 2024
d5a89d9
Back out "Chapter 7 - Wrap all `<Listing>`s to comply with the virtua…
chriskrycho Oct 15, 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
18 changes: 11 additions & 7 deletions src/ch07-02-defining-modules-to-control-scope-and-privacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,25 @@ backyard

The crate root file in this case is *src/main.rs*, and it contains:

<span class="filename">Filename: src/main.rs</span>
<Listing file-name="src/main.rs">

```rust,noplayground,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/quick-reference-example/src/main.rs}}
```

</Listing>

The `pub mod garden;` line tells the compiler to include the code it finds in
*src/garden.rs*, which is:

<span class="filename">Filename: src/garden.rs</span>
<Listing file-name="src/garden.rs">

```rust,noplayground,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/quick-reference-example/src/garden.rs}}
```

</Listing>

Here, `pub mod vegetables;` means the code in *src/garden/vegetables.rs* is
included too. That code is:

Expand Down Expand Up @@ -114,14 +118,13 @@ 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 +146,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 +160,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
41 changes: 18 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,13 @@ 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 +69,13 @@ 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 +105,24 @@ 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 +140,13 @@ 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 +214,13 @@ 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 +244,13 @@ 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 +268,13 @@ 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
58 changes: 26 additions & 32 deletions src/ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ scope of the `eat_at_restaurant` function so we only have to specify
`hosting::add_to_waitlist` to call the `add_to_waitlist` function in
`eat_at_restaurant`.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-11" file-name="src/lib.rs" caption="Bringing a module into scope with `use`">

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

<span class="caption">Listing 7-11: Bringing a module into scope with
`use`</span>
</Listing>

Adding `use` and a path in a scope is similar to creating a symbolic link in
the filesystem. By adding `use crate::front_of_house::hosting` in the crate
Expand All @@ -32,14 +31,13 @@ Note that `use` only creates the shortcut for the particular scope in which the
child module named `customer`, which is then a different scope than the `use`
statement, so the function body won’t compile.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-12" file-name="src/lib.rs" caption="A `use` statement only applies in the scope it’s in">

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

<span class="caption">Listing 7-12: A `use` statement only applies in the scope
it’s in</span>
</Listing>

The compiler error shows that the shortcut no longer applies within the
`customer` module:
Expand All @@ -60,14 +58,13 @@ crate::front_of_house::hosting` and then called `hosting::add_to_waitlist` in
`eat_at_restaurant`, rather than specifying the `use` path all the way out to
the `add_to_waitlist` function to achieve the same result, as in Listing 7-13.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-13" file-name="src/lib.rs" caption="Bringing the `add_to_waitlist` function into scope with `use`, which is unidiomatic">

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

<span class="caption">Listing 7-13: Bringing the `add_to_waitlist` function
into scope with `use`, which is unidiomatic</span>
</Listing>

Although both Listing 7-11 and Listing 7-13 accomplish the same task, Listing
7-11 is the idiomatic way to bring a function into scope with `use`. Bringing
Expand All @@ -82,14 +79,13 @@ it’s idiomatic to specify the full path. Listing 7-14 shows the idiomatic way
to bring the standard library’s `HashMap` struct into the scope of a binary
crate.

<span class="filename">Filename: src/main.rs</span>
<Listing number="7-14" file-name="src/main.rs" caption="Bringing `HashMap` into scope in an idiomatic way">

```rust
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-14/src/main.rs}}
```

<span class="caption">Listing 7-14: Bringing `HashMap` into scope in an
idiomatic way</span>
</Listing>

There’s no strong reason behind this idiom: it’s just the convention that has
emerged, and folks have gotten used to reading and writing Rust code this way.
Expand All @@ -99,14 +95,13 @@ into scope with `use` statements, because Rust doesn’t allow that. Listing 7-1
shows how to bring two `Result` types into scope that have the same name but
different parent modules, and how to refer to them.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-15" file-name="src/lib.rs" caption="Bringing two types with the same name into the same scope requires using their parent modules.">

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

<span class="caption">Listing 7-15: Bringing two types with the same name into
the same scope requires using their parent modules.</span>
</Listing>

As you can see, using the parent modules distinguishes the two `Result` types.
If instead we specified `use std::fmt::Result` and `use std::io::Result`, we’d
Expand All @@ -120,14 +115,13 @@ into the same scope with `use`: after the path, we can specify `as` and a new
local name, or *alias*, for the type. Listing 7-16 shows another way to write
the code in Listing 7-15 by renaming one of the two `Result` types using `as`.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-16" file-name="src/lib.rs" caption="Renaming a type when it’s brought into scope with the `as` keyword">

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

<span class="caption">Listing 7-16: Renaming a type when it’s brought into
scope with the `as` keyword</span>
</Listing>

In the second `use` statement, we chose the new name `IoResult` for the
`std::io::Result` type, which won’t conflict with the `Result` from `std::fmt`
Expand All @@ -146,14 +140,13 @@ their scope.
Listing 7-17 shows the code in Listing 7-11 with `use` in the root module
changed to `pub use`.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-17" file-name="src/lib.rs" caption="Making a name available for any code to use from a new scope with `pub use`">

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

<span class="caption">Listing 7-17: Making a name available for any code to use
from a new scope with `pub use`</span>
</Listing>

Before this change, external code would have to call the `add_to_waitlist`
function by using the path
Expand Down Expand Up @@ -186,12 +179,14 @@ added this line to *Cargo.toml*:
* ch14-03-cargo-workspaces.md
-->

<span class="filename">Filename: Cargo.toml</span>
<Listing file-name="Cargo.toml">

```toml
{{#include ../listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml:9:}}
```

</Listing>

Adding `rand` as a dependency in *Cargo.toml* tells Cargo to download the
`rand` package and any dependencies from [crates.io](https://crates.io/) and
make `rand` available to our project.
Expand Down Expand Up @@ -231,25 +226,26 @@ each item on its own line can take up a lot of vertical space in our files. For
example, these two `use` statements we had in the guessing game in Listing 2-4
bring items from `std` into scope:

<span class="filename">Filename: src/main.rs</span>
<Listing file-name="src/main.rs">

```rust,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/no-listing-01-use-std-unnested/src/main.rs:here}}
```

</Listing>

Instead, we can use nested paths to bring the same items into scope in one
line. We do this by specifying the common part of the path, followed by two
colons, and then curly brackets around a list of the parts of the paths that
differ, as shown in Listing 7-18.

<span class="filename">Filename: src/main.rs</span>
<Listing number="7-18" file-name="src/main.rs" caption="Specifying a nested path to bring multiple items with the same prefix into scope">

```rust,ignore
{{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-18/src/main.rs:here}}
```

<span class="caption">Listing 7-18: Specifying a nested path to bring multiple
items with the same prefix into scope</span>
</Listing>

In bigger programs, bringing many items into scope from the same crate or
module using nested paths can reduce the number of separate `use` statements
Expand All @@ -260,27 +256,25 @@ two `use` statements that share a subpath. For example, Listing 7-19 shows two
`use` statements: one that brings `std::io` into scope and one that brings
`std::io::Write` into scope.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-19" file-name="src/lib.rs" caption="Two `use` statements where one is a subpath of the other">

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

<span class="caption">Listing 7-19: Two `use` statements where one is a subpath
of the other</span>
</Listing>

The common part of these two paths is `std::io`, and that’s the complete first
path. To merge these two paths into one `use` statement, we can use `self` in
the nested path, as shown in Listing 7-20.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="7-20" file-name="src/lib.rs" caption="Combining the paths in Listing 7-19 into one `use` statement">

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

<span class="caption">Listing 7-20: Combining the paths in Listing 7-19 into
one `use` statement</span>
</Listing>

This line brings `std::io` and `std::io::Write` into scope.

Expand Down
Loading