|
1 | 1 | 当你写大型的程序的时候,如果去组织你的代码会变的非常重要,因为你不可能能单纯靠着脑子去跟踪你的代码。通过吧相关的功能都组织起来,以及吧单独的功能给拆分开,找到的你一个特别的功能可以就变得很清晰,很容易了,
|
2 |
| -The programs we’ve written so far have been in one module in one file. As a project grows, you can organize code by splitting it into multiple modules and then multiple files. A package can contain multiple binary crates and optionally one library crate. As a package grows, you can extract parts into separate crates that become external dependencies. This chapter covers all these techniques. For very large projects of a set of interrelated packages that evolve together, Cargo provides workspaces, which we’ll cover in the “Cargo Workspaces” section in Chapter 14. |
| 2 | + |
| 3 | +就目前为止的我们所写的代码都是在一个文件里的一个模块,随着项目的开发,你可以通过把代码拆分成不同的模块然后分成不同的文件来组织你的代码。一个包可以包含多个bin crate,也可以仅仅包含一个lib crate。随着包的开发,你可以把不同的模块的代码提取到不同过的 crate中,接下来变成外部依赖。这章涉及所有的这些技术。 |
| 4 | + |
| 5 | +通过新增给功能的归组,封装实现细节会让你在更高的级别重用代码:一旦你实现了一个操作,其他的代码可以通过调用 public 方法来使用,而不用知道内部的实现的细节。 |
3 | 6 |
|
4 | 7 | In addition to grouping functionality, encapsulating implementation details lets you reuse code at a higher level: once you’ve implemented an operation, other code can call that code via the code’s public interface without knowing how the implementation works. The way you write code defines which parts are public for other code to use and which parts are private implementation details that you reserve the right to change. This is another way to limit the amount of detail you have to keep in your head.
|
5 | 8 |
|
6 | 9 | A related concept is scope: the nested context in which code is written has a set of names that are defined as “in scope.” When reading, writing, and compiling code, programmers and compilers need to know whether a particular name at a particular spot refers to a variable, function, struct, enum, module, constant, or other item and what that item means. You can create scopes and change which names are in or out of scope. You can’t have two items with the same name in the same scope; tools are available to resolve name conflicts.
|
7 | 10 |
|
8 | 11 | Rust has a number of features that allow you to manage your code’s organization, including which details are exposed, which details are private, and what names are in each scope in your programs. These features, sometimes collectively referred to as the module system, include:
|
9 |
| -- Packages: A Cargo feature that lets you build, test, and share crates |
10 |
| -- Crates: A tree of modules that produces a library or executable |
11 |
| -- Modules and use: Let you control the organization, scope, and privacy of paths |
12 |
| -- Paths: A way of naming an item, such as a struct, function, or module |
| 12 | +- Packages(包): A Cargo feature that lets you build, test, and share crates |
| 13 | +- Crates(板条箱): A tree of modules that produces a library or executable |
| 14 | +- **Modules** and **use** (模块以及use): Let you control the organization, scope, and privacy of paths |
| 15 | +- Paths(路径): A way of naming an item, such as a struct, function, or module |
13 | 16 |
|
| 17 | +在这章中,我们将会涉及所有这些功能,讨论如何将他们联系起来 |
14 | 18 | In this chapter, we’ll cover all these features, discuss how they interact, and explain how to use them to manage scope. By the end, you should have a solid understanding of the module system and be able to work with scopes like a pro!
|
15 | 19 |
|
16 |
| - |
17 | 20 | # 1 包 和 箱 Packages and Crates
|
18 |
| -我们介绍的模块系统的第一部分是 包(packages) 和 箱(crates),一个 箱 可以是一个 二进制文件(binary file) 或者 库(libiary)。箱(crate)的根目录是一个是 Rust编译器开始运行的代码文件,以及组成了箱的组成模块。一个包是一个或者多个的提供功能的箱。一个包(package)包含一个 `Cargo.toml` 文件,这个文件描述了如何构建这些箱。 |
19 |
| -有几个规则规定了一个包(package)里可以包含什么。一个包必须包含0个 或者 1个的代码库,并且不能包含更多的代码库了,他可以包含任意数量的二进制箱子,但是他必须包含至少一个箱。 |
| 21 | +我们介绍的模块系统的第一部分是 包(packages) 和 箱(crates),一个 箱(crate) 可以是一个 二进制文件(binary crate) 或者 库(libiary crate)。箱(crate)的 *root* 是一个是 Rust编译器开始运行的代码文件,以及组成了箱的组成模块。一个 *包(package)* 是一个或者多个的提供功能的箱(crate)。一个包(package)包含一个 `Cargo.toml` 文件,这个文件描述了如何构建这些箱。 |
| 22 | + |
| 23 | +有几个规则规定了一个包(package)里可以包含什么。一个包(package) *必须* 包含0个 或者 1个 library crate ,并且不能有超过一个。包(package)可以包含任意数量的binary crate,但是必须包含至少一个 crate。 |
| 24 | + |
20 | 25 | 让我们来看看我们创建一个代码包的时候,我们会发生什么?首先,我们输入一个命令:`cargo new`:
|
21 | 26 | ```shell
|
22 | 27 | $ cargo new my-project
|
|
27 | 32 | $ ls my-project/src
|
28 | 33 | main.rs
|
29 | 34 | ```
|
30 |
| -输入命令之后,`Cargo` 创建了一个文件 `Carog.toml`,以及创建了一个代码包(package)。先来看看这个 `toml` 的文件里的内容,在文件里并没有提及 `src/mian.rs` 文件,这是因为 `Cargo` 遵循一个规则,就是 `src/main.sr` 是与包同名的二进制箱的根(crate root)。这样, Car就知道的如果 包的文件夹下面包含 文件 `src/lib.rs`,这个包含一个就包含了一个同名箱的根。`Cargo` 将箱的文件传递给 `rustc`,用于创建 文件库或者二进制文件。 |
| 35 | +输入命令之后,`Cargo` 创建了一个文件 `Carog.toml`,以及创建了一个代码包(package)。先来看看这个 `toml` 的文件里的内容,在文件里并没有提及 `src/mian.rs` 文件,这是因为 `Cargo` 遵循一个规则,就是 `src/main.sr` 是与包(package)同名的二进制箱(binary)的根(crate root)。这样, Car就知道的如果 包的文件夹下面包含 文件 `src/lib.rs`,这个包含一个就包含了一个同名箱的根。`Cargo` 将箱的文件传递给 `rustc`,用于创建 文件库或者二进制文件。 |
31 | 36 | 此时,我们只有一个仅仅包含了 `src/main.rs`的包,这个就意味着它仅仅包含了一个命名为 `my-project` 的二进制箱,如果一个包含 `src/main.rs` 和 `src/lib.rs`,那么它就有了两个箱:一个库和一个二进制文件,两个的箱的名称和包的名称相同。通过吧文件放在 `src/bin` 的文件夹下面,1个软件包就可以具有多个二进制的文件,每个文件都是一个单独的文件箱。
|
32 | 37 |
|
33 | 38 | 箱会把一个作用域里的所用的相关的功能组合在一起,因此该功能就可以在不同的项目之间共享。通过把箱导入我们项目范围,比如,我们在第二章中用的 `rand` 的箱提供可以可以生成随机数能给你。通过把包导入倒我们的项目中,就可以通过 `rand` 的箱来访问 `rand` 的箱提供的功能了。
|
|
0 commit comments