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
+
1
7
[ TOC]
2
8
3
9
# 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!
50
56
51
57
## Packages and Crates
52
58
59
+ <!-- Liz: I reorganized this section a little bit. /Carol -->
60
+
53
61
The first parts of the module system we’ll cover are packages and crates.
54
62
55
63
A * package* is one or more crates that provide a set of functionality. A
@@ -63,17 +71,16 @@ far have been binary crates.
63
71
64
72
* Library crates* don’t have a ` main ` function, and they don’t compile to an
65
73
executable. 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.
67
76
68
77
The * crate root* is a source file that the Rust compiler starts from and makes
69
78
up 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).
72
80
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).
77
84
78
85
Let’s walk through what happens when we create a package. First, we enter the
79
86
command ` cargo new ` :
@@ -589,9 +596,9 @@ I wanted to add a bit of a call-out for. /Carol -->
589
596
> This helps you design a good API; not only are you the author, you’re also a
590
597
> client!
591
598
>
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.
595
602
596
603
### Starting Relative Paths with ` super `
597
604
@@ -762,6 +769,10 @@ root, `hosting` is now a valid name in that scope, just as though the `hosting`
762
769
module had been defined in the crate root. Paths brought into scope with ` use `
763
770
also check privacy, like any other paths.
764
771
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
+
765
776
Note that ` use ` only creates the shortcut for the particular scope in which the
766
777
` use ` occurs. Listing 7-12 moves the ` eat_at_restaurant ` function into a new
767
778
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
791
802
` customer ` module:
792
803
793
804
```
794
- $ cargo build
795
- Compiling restaurant v0.1.0 (file:///projects/restaurant)
796
805
error[E0433]: failed to resolve: use of undeclared crate or module `hosting`
797
806
--> src/lib.rs:11:9
798
807
|
@@ -806,10 +815,6 @@ warning: unused import: `crate::front_of_house::hosting`
806
815
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
807
816
|
808
817
= 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
813
818
```
814
819
815
820
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
983
988
package called ` rand ` to get random numbers. To use ` rand ` in our project, we
984
989
added this line to * Cargo.toml* :
985
990
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
-
992
991
Filename: Cargo.toml
993
992
994
993
```
@@ -1121,6 +1120,11 @@ So far, all the examples in this chapter defined multiple modules in one file.
1121
1120
When modules get large, you might want to move their definitions to a separate
1122
1121
file to make the code easier to navigate.
1123
1122
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
+
1124
1128
For example, let’s start from the code in Listing 7-17 and extract modules into
1125
1129
files instead of having all the modules defined in the crate root file. In this
1126
1130
case, the crate root file is * src/lib.rs* , but this procedure also works with
@@ -1163,14 +1167,20 @@ pub mod hosting {
1163
1167
Listing 7-22: Definitions inside the ` front_of_house ` module in
1164
1168
* src/front_of_house.rs*
1165
1169
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
+
1166
1176
Note that you only need to load the contents of a file using a ` mod `
1167
1177
declaration once somewhere in your module tree. Once the compiler knows the
1168
1178
file is part of the project (and knows where in the module tree the code
1169
1179
resides because of where you’ve put the ` mod ` statement), other files in your
1170
1180
project 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.
1174
1184
1175
1185
Next, we’ll extract the ` hosting ` module to its own file as well. The process
1176
1186
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
1202
1212
what files to look in for modules’ code means the directories and files more
1203
1213
closely match the module tree.
1204
1214
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
+
1205
1219
> ### Alternate File Paths
1206
1220
>
1207
1221
> This section covered the most idiomatic file paths the Rust compiler uses;
0 commit comments