From ea90bbaf53ba64ef4e2da9ac2352b298aec6bec8 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 28 Mar 2022 21:56:18 -0400 Subject: [PATCH] Fix nostarch snapshot --- nostarch/chapter07.md | 64 ++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/nostarch/chapter07.md b/nostarch/chapter07.md index 71208e39c..d3f0e113f 100644 --- a/nostarch/chapter07.md +++ b/nostarch/chapter07.md @@ -1,3 +1,9 @@ + + [TOC] # Managing Growing Projects with Packages, Crates, and Modules @@ -50,6 +56,8 @@ understanding of the module system and be able to work with scopes like a pro! ## Packages and Crates + + The first parts of the module system we’ll cover are packages and crates. A *package* is one or more crates that provide a set of functionality. A @@ -63,17 +71,16 @@ far have been binary crates. *Library crates* don’t have a `main` function, and they don’t compile to an executable. They define functionality intended to be shared with multiple -projects. For example, the `rand` crate we used in Chapter 2 provides functionality that generates random numbers. +projects. For example, the `rand` crate we used in Chapter 2 provides +functionality that generates random numbers. The *crate root* is a source file that the Rust compiler starts from and makes up the root module of your crate (we’ll explain modules in depth in the -“Defining Modules to Control Scope and Privacy” -section). +“Defining Modules to Control Scope and Privacy” section). -Several rules determine what a package can contain. A package can contain -at most one library crate. It can contain as many binary crates -as you’d like, but it must contain at least one crate (either library or -binary). +Several rules determine what a package can contain. A package can contain at +most one library crate. It can contain as many binary crates as you’d like, but +it must contain at least one crate (either library or binary). Let’s walk through what happens when we create a package. First, we enter the command `cargo new`: @@ -589,9 +596,9 @@ I wanted to add a bit of a call-out for. /Carol --> > This helps you design a good API; not only are you the author, you’re also a > client! > -> In Chapter 12, we’ll demonstrate this organizational -> practice with a command-line program that will contain both a binary crate -> and a library crate. +> In Chapter 12, we’ll demonstrate this organizational practice with a +> command-line program that will contain both a binary crate and a library +> crate. ### Starting Relative Paths with `super` @@ -762,6 +769,10 @@ root, `hosting` is now a valid name in that scope, just as though the `hosting` module had been defined in the crate root. Paths brought into scope with `use` also check privacy, like any other paths. + + Note that `use` only creates the shortcut for the particular scope in which the `use` occurs. Listing 7-12 moves the `eat_at_restaurant` function into a new child module named `customer`, which is then a different scope than the `use` @@ -791,8 +802,6 @@ The compiler error shows that the shortcut no longer applies within the `customer` module: ``` -$ cargo build - Compiling restaurant v0.1.0 (file:///projects/restaurant) error[E0433]: failed to resolve: use of undeclared crate or module `hosting` --> src/lib.rs:11:9 | @@ -806,10 +815,6 @@ warning: unused import: `crate::front_of_house::hosting` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default - -For more information about this error, try `rustc --explain E0433`. -warning: `restaurant` (lib) generated 1 warning -error: could not compile `restaurant` due to previous error; 1 warning emitted ``` Notice there’s also a warning that the `use` is no longer used in its scope! To @@ -983,12 +988,6 @@ In Chapter 2, we programmed a guessing game project that used an external package called `rand` to get random numbers. To use `rand` in our project, we added this line to *Cargo.toml*: - - Filename: Cargo.toml ``` @@ -1121,6 +1120,11 @@ So far, all the examples in this chapter defined multiple modules in one file. When modules get large, you might want to move their definitions to a separate file to make the code easier to navigate. + + For example, let’s start from the code in Listing 7-17 and extract modules into files instead of having all the modules defined in the crate root file. In this case, the crate root file is *src/lib.rs*, but this procedure also works with @@ -1163,14 +1167,20 @@ pub mod hosting { Listing 7-22: Definitions inside the `front_of_house` module in *src/front_of_house.rs* + + Note that you only need to load the contents of a file using a `mod` declaration once somewhere in your module tree. Once the compiler knows the file is part of the project (and knows where in the module tree the code resides because of where you’ve put the `mod` statement), other files in your project should refer to the code in that file using a path to where it was -declared as covered in the “Paths for Referring to an Item in the Module -Tree” section. In other words, `mod` is *not* an -“include” operation that other programming languages have. +declared as covered in the “Paths for Referring to an Item in the Module Tree” +section. In other words, `mod` is *not* an “include” operation that other +programming languages have. Next, we’ll extract the `hosting` module to its own file as well. The process is a bit different because `hosting` is a child module of `front_of_house`, not @@ -1202,6 +1212,10 @@ a child of the `front_of_house` module. The rules the compiler follows to know what files to look in for modules’ code means the directories and files more closely match the module tree. + + > ### Alternate File Paths > > This section covered the most idiomatic file paths the Rust compiler uses;