Package、Crate 和模块
Packages, Crates, and Modules
当你编写大型程序时,组织代码将变得越来越重要。通过将相关功能分组并将具有不同特性的代码分离,你将明确在哪里可以找到实现特定功能的代码,以及在哪里可以更改功能的工作方式。
As you write large programs, organizing your code will become increasingly important. By grouping related functionality and separating code with distinct features, you’ll clarify where to find code that implements a particular feature and where to go to change how a feature works.
到目前为止,我们编写的程序都在一个文件中的一个模块中。随着项目的增长,你应该通过将代码拆分为多个模块然后再拆分为多个文件来组织代码。一个 package 可以包含多个二进制 crate,以及可选的一个库 crate。随着 package 的增长,你可以将其部分提取到单独的 crate 中,从而成为外部依赖项。本章涵盖了所有这些技术。对于由一组相互关联、共同发展的 package 组成的大型项目,Cargo 提供了 workspace(工作空间),我们将在第 14 章的“Cargo Workspace”中进行介绍。
The programs we’ve written so far have been in one module in one file. As a project grows, you should 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 comprising a set of interrelated packages that evolve together, Cargo provides workspaces, which we’ll cover in “Cargo Workspaces” in Chapter 14.
我们还将讨论封装实现细节,这允许你在更高层次上重用代码:一旦你实现了一个操作,其他代码就可以通过其公共接口调用你的代码,而无需知道实现是如何工作的。你编写代码的方式定义了哪些部分是公开给其他代码使用的,哪些部分是保留更改权的私有实现细节。这是限制你脑子里必须保留细节数量的另一种方法。
We’ll also discuss encapsulating implementation details, which lets you reuse code at a higher level: Once you’ve implemented an operation, other code can call your code via its public interface without having to know 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.
一个相关的概念是作用域:编写代码的嵌套上下文有一组被定义为“在作用域内”的名称。在阅读、编写和编译代码时,程序员和编译器需要知道特定位置的特定名称是指变量、函数、结构体、枚举、模块、常量还是其他项,以及该项的含义。你可以创建作用域,并更改哪些名称在作用域内或作用域外。在同一个作用域内不能有两个同名的项;可以使用工具来解决名称冲突。
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.
Rust 有许多功能允许你管理代码组织,包括公开哪些细节、哪些细节是私有的,以及程序中每个作用域内有哪些名称。这些功能有时统称为“模块系统”(module system),包括:
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:
-
Package(包):让你构建、测试和共享 crate 的 Cargo 功能。
-
Packages: A Cargo feature that lets you build, test, and share crates
-
Crate(单元包):产生库或可执行文件的模块树。
-
Crates: A tree of modules that produces a library or executable
-
模块(Module)和 use:让你控制路径的组织、作用域和隐私。
-
Modules and use: Let you control the organization, scope, and privacy of paths
-
路径(Path):命名项(如结构体、函数或模块)的方法。
-
Paths: A way of naming an item, such as a struct, function, or module
在本章中,我们将涵盖所有这些功能,讨论它们如何交互,并解释如何使用它们来管理作用域。到最后,你应该对模块系统有深刻的理解,并能像专家一样处理作用域!
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!