]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
1 | ## Defining Modules to Control Scope and Privacy |
2 | ||
3 | In this section, we’ll talk about modules and other parts of the module system, | |
4 | namely *paths* that allow you to name items; the `use` keyword that brings a | |
5 | path into scope; and the `pub` keyword to make items public. We’ll also discuss | |
dc9dc135 XL |
6 | the `as` keyword, external packages, and the glob operator. For now, let’s |
7 | focus on modules! | |
532ac7d7 XL |
8 | |
9 | *Modules* let us organize code within a crate into groups for readability and | |
10 | easy reuse. Modules also control the *privacy* of items, which is whether an | |
dc9dc135 XL |
11 | item can be used by outside code (*public*) or is an internal implementation |
12 | detail and not available for outside use (*private*). | |
532ac7d7 XL |
13 | |
14 | As an example, let’s write a library crate that provides the functionality of a | |
15 | restaurant. We’ll define the signatures of functions but leave their bodies | |
dc9dc135 XL |
16 | empty to concentrate on the organization of the code, rather than actually |
17 | implement a restaurant in code. | |
532ac7d7 | 18 | |
dc9dc135 XL |
19 | In 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 | |
21 | customers are; this is where hosts seat customers, servers take orders and | |
22 | payment, and bartenders make drinks. Back of house is where the chefs and cooks | |
23 | work in the kitchen, dishwashers clean up, and managers do administrative work. | |
532ac7d7 XL |
24 | |
25 | To structure our crate in the same way that a real restaurant works, we can | |
26 | organize the functions into nested modules. Create a new library named | |
27 | `restaurant` by running `cargo new --lib restaurant`; then put the code in | |
28 | Listing 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 |
33 | mod 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 | |
51 | modules that then contain functions</span> | |
52 | ||
dc9dc135 | 53 | We define a module by starting with the `mod` keyword and then specify the |
532ac7d7 XL |
54 | name of the module (in this case, `front_of_house`) and place curly brackets |
55 | around the body of the module. Inside modules, we can have other modules, as in | |
56 | this case with the modules `hosting` and `serving`. Modules can also hold | |
dc9dc135 XL |
57 | definitions for other items, such as structs, enums, constants, traits, or—as |
58 | in Listing 7-1—functions. | |
532ac7d7 XL |
59 | |
60 | By using modules, we can group related definitions together and name why | |
61 | they’re related. Programmers using this code would have an easier time finding | |
dc9dc135 XL |
62 | the definitions they wanted to use because they could navigate the code based |
63 | on the groups rather than having to read through all the definitions. | |
64 | Programmers adding new functionality to this code would know where to place the | |
65 | code to keep the program organized. | |
532ac7d7 | 66 | |
dc9dc135 XL |
67 | Earlier, we mentioned that *src/main.rs* and *src/lib.rs* are called crate |
68 | roots. The reason for their name is that the contents of either of these two | |
532ac7d7 XL |
69 | files form a module named `crate` at the root of the crate’s module structure, |
70 | known as the *module tree*. | |
71 | ||
72 | Listing 7-2 shows the module tree for the structure in Listing 7-1. | |
73 | ||
74 | ```text | |
75 | crate | |
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 | |
87 | 7-1</span> | |
88 | ||
dc9dc135 XL |
89 | This 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 |
91 | are *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 | |
93 | family metaphor, if module A is contained inside module B, we say that module A | |
dc9dc135 | 94 | is the *child* of module B and that module B is the *parent* of module A. |
532ac7d7 XL |
95 | Notice that the entire module tree is rooted under the implicit module named |
96 | `crate`. | |
97 | ||
98 | The module tree might remind you of the filesystem’s directory tree on your | |
99 | computer; this is a very apt comparison! Just like directories in a filesystem, | |
100 | you use modules to organize your code. And just like files in a directory, we | |
101 | need a way to find our modules. |