]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch07-05-separating-modules-into-different-files.md
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / src / doc / book / src / ch07-05-separating-modules-into-different-files.md
1 ## Separating Modules into Different Files
2
3 So far, all the examples in this chapter defined multiple modules in one file.
4 When modules get large, you might want to move their definitions to a separate
5 file to make the code easier to navigate.
6
7 For example, let’s start from the code in Listing 7-17 and move the
8 `front_of_house` module to its own file *src/front_of_house.rs* by changing the
9 crate root file so it contains the code shown in Listing 7-21. In this case,
10 the crate root file is *src/lib.rs*, but this procedure also works with binary
11 crates whose crate root file is *src/main.rs*.
12
13 <span class="filename">Filename: src/lib.rs</span>
14
15 ```rust,ignore
16 {{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-21-and-22/src/lib.rs}}
17 ```
18
19 <span class="caption">Listing 7-21: Declaring the `front_of_house` module whose
20 body will be in *src/front_of_house.rs*</span>
21
22 And *src/front_of_house.rs* gets the definitions from the body of the
23 `front_of_house` module, as shown in Listing 7-22.
24
25 <span class="filename">Filename: src/front_of_house.rs</span>
26
27 ```rust,ignore
28 {{#rustdoc_include ../listings/ch07-managing-growing-projects/listing-07-21-and-22/src/front_of_house.rs}}
29 ```
30
31 <span class="caption">Listing 7-22: Definitions inside the `front_of_house`
32 module in *src/front_of_house.rs*</span>
33
34 Using a semicolon after `mod front_of_house` rather than using a block tells
35 Rust to load the contents of the module from another file with the same name as
36 the module. To continue with our example and extract the `hosting` module to
37 its own file as well, we change *src/front_of_house.rs* to contain only the
38 declaration of the `hosting` module:
39
40 <span class="filename">Filename: src/front_of_house.rs</span>
41
42 ```rust,ignore
43 {{#rustdoc_include ../listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house.rs}}
44 ```
45
46 Then we create a *src/front_of_house* directory and a file
47 *src/front_of_house/hosting.rs* to contain the definitions made in the
48 `hosting` module:
49
50 <span class="filename">Filename: src/front_of_house/hosting.rs</span>
51
52 ```rust
53 {{#rustdoc_include ../listings/ch07-managing-growing-projects/no-listing-02-extracting-hosting/src/front_of_house/hosting.rs}}
54 ```
55
56 The module tree remains the same, and the function calls in `eat_at_restaurant`
57 will work without any modification, even though the definitions live in
58 different files. This technique lets you move modules to new files as they grow
59 in size.
60
61 Note that the `pub use crate::front_of_house::hosting` statement in
62 *src/lib.rs* also hasn’t changed, nor does `use` have any impact on what files
63 are compiled as part of the crate. The `mod` keyword declares modules, and Rust
64 looks in a file with the same name as the module for the code that goes into
65 that module.
66
67 ## Summary
68
69 Rust lets you split a package into multiple crates and a crate into modules
70 so you can refer to items defined in one module from another module. You can do
71 this by specifying absolute or relative paths. These paths can be brought into
72 scope with a `use` statement so you can use a shorter path for multiple uses of
73 the item in that scope. Module code is private by default, but you can make
74 definitions public by adding the `pub` keyword.
75
76 In the next chapter, we’ll look at some collection data structures in the
77 standard library that you can use in your neatly organized code.