x-i18n: generated_at: “2026-03-01T13:50:32Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: d5dcbe4f7799c942a95051d313df3b70383a1835cff1fb71510c027c4581a242 source_path: ch07-00-managing-growing-projects-with-packages-crates-and-modules.md workflow: 16
包、Crate 与模块 (Packages, Crates, and Modules)
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。随着包的增长,你可以将部分内容提取到单独的 crate 中,从而成为外部依赖项。本章涵盖了所有这些技术。对于由一组相互关联且共同发展的包组成的超大型项目,Cargo 提供了工作空间(workspaces),我们将在第 14 章的“Cargo 工作空间”中介绍。
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.
一个相关的概念是作用域(scope):编写代码的嵌套上下文具有一组被定义为“在作用域内”的名称。在读取、编写和编译代码时,程序员和编译器需要知道特定位置的特定名称是指变量、函数、结构体、枚举、模块、常量还是其他项,以及该项意味着什么。你可以创建作用域并更改哪些名称在作用域内或作用域外。在同一个作用域内不能有两个同名的项;可以使用工具来解决名称冲突。
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:
-
包 (Packages):Cargo 的一个功能,允许你构建、测试和分享 crate
-
Crate (Crates):产生库或可执行文件的模块树
-
模块与 use (Modules and use):允许你控制路径的组织、作用域和私有性
-
路径 (Paths):命名项(如结构体、函数或模块)的一种方式
-
Packages: A Cargo feature that lets you build, test, and share crates
-
Crates: A tree of modules that produces a library or executable
-
Modules and use: Let you control the organization, scope, and privacy of paths
-
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!