]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch07-00-managing-growing-projects-with-packages-crates-and-modules.md
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / book / src / ch07-00-managing-growing-projects-with-packages-crates-and-modules.md
CommitLineData
532ac7d7
XL
1# Managing Growing Projects with Packages, Crates, and Modules
2
923072b8
FG
3As you write large programs, organizing your code will become increasingly
4important. By grouping related functionality and separating code with distinct
5features, you’ll clarify where to find code that implements a particular
6feature and where to go to change how a feature works.
532ac7d7
XL
7
8The programs we’ve written so far have been in one module in one file. As a
923072b8
FG
9project grows, you should organize code by splitting it into multiple modules
10and then multiple files. A package can contain multiple binary crates and
532ac7d7
XL
11optionally one library crate. As a package grows, you can extract parts into
12separate crates that become external dependencies. This chapter covers all
923072b8
FG
13these techniques. For very large projects comprising a set of interrelated
14packages that evolve together, Cargo provides *workspaces*, which we’ll cover
15in the [“Cargo Workspaces”][workspaces]<!-- ignore --> section in Chapter 14.
532ac7d7 16
923072b8
FG
17We’ll also discuss encapsulating implementation details, which lets you reuse
18code at a higher level: once you’ve implemented an operation, other code can
19call your code via its public interface without having to know how the
20implementation works. The way you write code defines which parts are public for
21other code to use and which parts are private implementation details that you
22reserve the right to change. This is another way to limit the amount of detail
23you have to keep in your head.
532ac7d7 24
dc9dc135
XL
25A related concept is scope: the nested context in which code is written has a
26set of names that are defined as “in scope.” When reading, writing, and
27compiling code, programmers and compilers need to know whether a particular
28name at a particular spot refers to a variable, function, struct, enum, module,
29constant, or other item and what that item means. You can create scopes and
30change which names are in or out of scope. You can’t have two items with the
31same name in the same scope; tools are available to resolve name conflicts.
532ac7d7
XL
32
33Rust has a number of features that allow you to manage your code’s
dc9dc135
XL
34organization, including which details are exposed, which details are private,
35and what names are in each scope in your programs. These features, sometimes
dfeec247 36collectively referred to as the *module system*, include:
532ac7d7
XL
37
38* **Packages:** A Cargo feature that lets you build, test, and share crates
39* **Crates:** A tree of modules that produces a library or executable
40* **Modules** and **use:** Let you control the organization, scope, and
41 privacy of paths
42* **Paths:** A way of naming an item, such as a struct, function, or module
43
44In this chapter, we’ll cover all these features, discuss how they interact, and
45explain how to use them to manage scope. By the end, you should have a solid
46understanding of the module system and be able to work with scopes like a pro!
47
48[workspaces]: ch14-03-cargo-workspaces.html