1+ <!-- DO NOT EDIT THIS FILE.
2+
3+ This file is periodically generated from the content in the `/src/`
4+ directory, so all fixes need to be made in `/src/`.
5+ -->
6+
17[ TOC]
28
39# 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!
5056
5157## Packages and Crates
5258
59+ <!-- Liz: I reorganized this section a little bit. /Carol -->
60+
5361The first parts of the module system we’ll cover are packages and crates.
5462
5563A * package* is one or more crates that provide a set of functionality. A
@@ -63,17 +71,16 @@ far have been binary crates.
6371
6472* Library crates* don’t have a ` main ` function, and they don’t compile to an
6573executable. They define functionality intended to be shared with multiple
66- projects. For example, the ` rand ` crate we used in Chapter 2 provides functionality that generates random numbers.
74+ projects. For example, the ` rand ` crate we used in Chapter 2 provides
75+ functionality that generates random numbers.
6776
6877The * crate root* is a source file that the Rust compiler starts from and makes
6978up the root module of your crate (we’ll explain modules in depth in the
70- “Defining Modules to Control Scope and Privacy”
71- section).
79+ “Defining Modules to Control Scope and Privacy” section).
7280
73- Several rules determine what a package can contain. A package can contain
74- at most one library crate. It can contain as many binary crates
75- as you’d like, but it must contain at least one crate (either library or
76- binary).
81+ Several rules determine what a package can contain. A package can contain at
82+ most one library crate. It can contain as many binary crates as you’d like, but
83+ it must contain at least one crate (either library or binary).
7784
7885Let’s walk through what happens when we create a package. First, we enter the
7986command ` cargo new ` :
@@ -589,9 +596,9 @@ I wanted to add a bit of a call-out for. /Carol -->
589596> This helps you design a good API; not only are you the author, you’re also a
590597> client!
591598>
592- > In Chapter 12, we’ll demonstrate this organizational
593- > practice with a command-line program that will contain both a binary crate
594- > and a library crate.
599+ > In Chapter 12, we’ll demonstrate this organizational practice with a
600+ > command-line program that will contain both a binary crate and a library
601+ > crate.
595602
596603### Starting Relative Paths with ` super `
597604
@@ -762,6 +769,10 @@ root, `hosting` is now a valid name in that scope, just as though the `hosting`
762769module had been defined in the crate root. Paths brought into scope with ` use `
763770also check privacy, like any other paths.
764771
772+ <!-- Liz: This next example is new and demonstrates something readers reported
773+ having issues with. The previous Listing 7-12 didn't feel like it was pulling
774+ its weight. /Carol -->
775+
765776Note that ` use ` only creates the shortcut for the particular scope in which the
766777` use ` occurs. Listing 7-12 moves the ` eat_at_restaurant ` function into a new
767778child 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
791802` customer ` module:
792803
793804```
794- $ cargo build
795- Compiling restaurant v0.1.0 (file:///projects/restaurant)
796805error[E0433]: failed to resolve: use of undeclared crate or module `hosting`
797806 --> src/lib.rs:11:9
798807 |
@@ -806,10 +815,6 @@ warning: unused import: `crate::front_of_house::hosting`
806815 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
807816 |
808817 = note: `#[warn(unused_imports)]` on by default
809-
810- For more information about this error, try `rustc --explain E0433`.
811- warning: `restaurant` (lib) generated 1 warning
812- error: could not compile `restaurant` due to previous error; 1 warning emitted
813818```
814819
815820Notice 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
983988package called ` rand ` to get random numbers. To use ` rand ` in our project, we
984989added this line to * Cargo.toml* :
985990
986- <!-- When updating the version of `rand` used, also update the version of
987- `rand` used in these files so they all match:
988- * ch02-00-guessing-game-tutorial.md
989- * ch14-03-cargo-workspaces.md
990- -->
991-
992991Filename: Cargo.toml
993992
994993```
@@ -1121,6 +1120,11 @@ So far, all the examples in this chapter defined multiple modules in one file.
11211120When modules get large, you might want to move their definitions to a separate
11221121file to make the code easier to navigate.
11231122
1123+ <!-- Liz: I tweaked the explanation of how and where to move the code around in
1124+ this section a bit; some readers ended up with code that didn't compile because
1125+ the last explanation wasn't as clear as it could have been. Please do try
1126+ following these instructions! /Carol -->
1127+
11241128For example, let’s start from the code in Listing 7-17 and extract modules into
11251129files instead of having all the modules defined in the crate root file. In this
11261130case, the crate root file is * src/lib.rs* , but this procedure also works with
@@ -1163,14 +1167,20 @@ pub mod hosting {
11631167Listing 7-22: Definitions inside the ` front_of_house ` module in
11641168* src/front_of_house.rs*
11651169
1170+ <!-- Liz: This next paragraph calls out a frequent problem people have when
1171+ they assume Rust works like other programming languages they're already
1172+ familiar with. Let me know if you think this is a good place for this or if it
1173+ would fit better somewhere else? And if it seems like it would still make sense
1174+ whether or not you're personally having this problem? /Carol -->
1175+
11661176Note that you only need to load the contents of a file using a ` mod `
11671177declaration once somewhere in your module tree. Once the compiler knows the
11681178file is part of the project (and knows where in the module tree the code
11691179resides because of where you’ve put the ` mod ` statement), other files in your
11701180project should refer to the code in that file using a path to where it was
1171- declared as covered in the “Paths for Referring to an Item in the Module
1172- Tree” section. In other words, ` mod ` is * not* an
1173- “include” operation that other programming languages have.
1181+ declared as covered in the “Paths for Referring to an Item in the Module Tree”
1182+ section. In other words, ` mod ` is * not* an “include” operation that other
1183+ programming languages have.
11741184
11751185Next, we’ll extract the ` hosting ` module to its own file as well. The process
11761186is 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
12021212what files to look in for modules’ code means the directories and files more
12031213closely match the module tree.
12041214
1215+ <!-- Liz: This new box is a topic some readers were surprised wasn't covered at
1216+ all, so I wanted to put in a quick mention because some people prefer this
1217+ structure even though it's not what most projects do. /Carol -->
1218+
12051219> ### Alternate File Paths
12061220>
12071221> This section covered the most idiomatic file paths the Rust compiler uses;
0 commit comments