]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch07-02-defining-modules-to-control-scope-and-privacy.md
New upstream version 1.41.1+dfsg1
[rustc.git] / src / doc / book / src / ch07-02-defining-modules-to-control-scope-and-privacy.md
CommitLineData
532ac7d7
XL
1## Defining Modules to Control Scope and Privacy
2
3In this section, we’ll talk about modules and other parts of the module system,
4namely *paths* that allow you to name items; the `use` keyword that brings a
5path into scope; and the `pub` keyword to make items public. We’ll also discuss
dc9dc135
XL
6the `as` keyword, external packages, and the glob operator. For now, let’s
7focus on modules!
532ac7d7
XL
8
9*Modules* let us organize code within a crate into groups for readability and
10easy reuse. Modules also control the *privacy* of items, which is whether an
dc9dc135
XL
11item can be used by outside code (*public*) or is an internal implementation
12detail and not available for outside use (*private*).
532ac7d7
XL
13
14As an example, let’s write a library crate that provides the functionality of a
15restaurant. We’ll define the signatures of functions but leave their bodies
dc9dc135
XL
16empty to concentrate on the organization of the code, rather than actually
17implement a restaurant in code.
532ac7d7 18
dc9dc135
XL
19In the restaurant industry, some parts of a restaurant are referred to as
20*front of house* and others as *back of house*. Front of house is where
21customers are; this is where hosts seat customers, servers take orders and
22payment, and bartenders make drinks. Back of house is where the chefs and cooks
23work in the kitchen, dishwashers clean up, and managers do administrative work.
532ac7d7
XL
24
25To structure our crate in the same way that a real restaurant works, we can
26organize the functions into nested modules. Create a new library named
27`restaurant` by running `cargo new --lib restaurant`; then put the code in
28Listing 7-1 into *src/lib.rs* to define some modules and function signatures.
29
30<span class="filename">Filename: src/lib.rs</span>
31
60c5eb7d 32```rust,ignore
532ac7d7
XL
33mod front_of_house {
34 mod hosting {
35 fn add_to_waitlist() {}
36
37 fn seat_at_table() {}
38 }
39
40 mod serving {
41 fn take_order() {}
42
43 fn serve_order() {}
44
45 fn take_payment() {}
46 }
47}
48```
49
50<span class="caption">Listing 7-1: A `front_of_house` module containing other
51modules that then contain functions</span>
52
dc9dc135 53We define a module by starting with the `mod` keyword and then specify the
532ac7d7
XL
54name of the module (in this case, `front_of_house`) and place curly brackets
55around the body of the module. Inside modules, we can have other modules, as in
56this case with the modules `hosting` and `serving`. Modules can also hold
dc9dc135
XL
57definitions for other items, such as structs, enums, constants, traits, or—as
58in Listing 7-1—functions.
532ac7d7
XL
59
60By using modules, we can group related definitions together and name why
61they’re related. Programmers using this code would have an easier time finding
dc9dc135
XL
62the definitions they wanted to use because they could navigate the code based
63on the groups rather than having to read through all the definitions.
64Programmers adding new functionality to this code would know where to place the
65code to keep the program organized.
532ac7d7 66
dc9dc135
XL
67Earlier, we mentioned that *src/main.rs* and *src/lib.rs* are called crate
68roots. The reason for their name is that the contents of either of these two
532ac7d7
XL
69files form a module named `crate` at the root of the crate’s module structure,
70known as the *module tree*.
71
72Listing 7-2 shows the module tree for the structure in Listing 7-1.
73
74```text
75crate
76 └── front_of_house
77 ├── hosting
78 │ ├── add_to_waitlist
79 │ └── seat_at_table
80 └── serving
81 ├── take_order
82 ├── serve_order
83 └── take_payment
84```
85
86<span class="caption">Listing 7-2: The module tree for the code in Listing
877-1</span>
88
dc9dc135
XL
89This tree shows how some of the modules nest inside one another (for example,
90`hosting` nests inside `front_of_house`). The tree also shows that some modules
532ac7d7
XL
91are *siblings* to each other, meaning they’re defined in the same module
92(`hosting` and `serving` are defined within `front_of_house`). To continue the
93family metaphor, if module A is contained inside module B, we say that module A
dc9dc135 94is the *child* of module B and that module B is the *parent* of module A.
532ac7d7
XL
95Notice that the entire module tree is rooted under the implicit module named
96`crate`.
97
98The module tree might remind you of the filesystem’s directory tree on your
99computer; this is a very apt comparison! Just like directories in a filesystem,
100you use modules to organize your code. And just like files in a directory, we
101need a way to find our modules.