x-i18n: generated_at: “2026-03-01T09:14:11Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 4f116f147d8f51119240b8aa235be830b86cf1d5f12ebca21c8ceeaa7bcfa73f source_path: appendix-05-editions.md workflow: 16
附录 E:版本 (Appendix E: Editions)
Appendix E: Editions
在第 1 章中,你看到 cargo new 在你的 Cargo.toml 文件中添加了一些关于版本的元数据。这个附录将讨论这代表着什么!
In Chapter 1, you saw that cargo new adds a bit of metadata to your
Cargo.toml file about an edition. This appendix talks about what that means!
Rust 语言和编译器有一个六周的发布周期,这意味着用户可以不断获得新功能。其他编程语言发布较大更改的频率较低;Rust 发布较小更新的频率更高。一段时间后,所有这些微小的变化都会累积起来。但是,在不同的发布版本之间,很难回顾并说:“哇,从 Rust 1.10 到 Rust 1.31,Rust 发生了很大变化!”
The Rust language and compiler have a six-week release cycle, meaning users get a constant stream of new features. Other programming languages release larger changes less often; Rust releases smaller updates more frequently. After a while, all of these tiny changes add up. But from release to release, it can be difficult to look back and say, “Wow, between Rust 1.10 and Rust 1.31, Rust has changed a lot!”
大约每三年,Rust 团队会制作一个新的 Rust 版本 (edition)。每个版本都将已经上线的功能整合到一个清晰的软件包中,并带有完整更新的文档和工具。新版本作为通常的六周发布过程的一部分发布。
Every three years or so, the Rust team produces a new Rust edition. Each edition brings together the features that have landed into a clear package with fully updated documentation and tooling. New editions ship as part of the usual six-week release process.
版本对不同的人有不同的目的:
Editions serve different purposes for different people:
- 对于活跃的 Rust 用户,新版本将渐进式的变化整合到一个易于理解的包中。
- For active Rust users, a new edition brings together incremental changes into an easy-to-understand package.
- 对于非用户,新版本标志着一些重大进步已经上线,这可能使 Rust 值得再次关注。
- For non-users, a new edition signals that some major advancements have landed, which might make Rust worth another look.
- 对于那些开发 Rust 的人来说,新版本为整个项目提供了一个凝聚点。
- For those developing Rust, a new edition provides a rallying point for the project as a whole.
在撰写本文时,已有四个 Rust 版本可用:Rust 2015、Rust 2018、Rust 2021 和 Rust 2024。本书使用 Rust 2024 版本的惯用写法编写。
At the time of this writing, four Rust editions are available: Rust 2015, Rust 2018, Rust 2021, and Rust 2024. This book is written using Rust 2024 edition idioms.
Cargo.toml 中的 edition 键指示编译器应为你的代码使用哪个版本。如果该键不存在,出于向后兼容性的原因,Rust 使用 2015 作为版本值。
The edition key in Cargo.toml indicates which edition the compiler should
use for your code. If the key doesn’t exist, Rust uses 2015 as the edition
value for backward compatibility reasons.
每个项目都可以选择使用默认 2015 版本以外的其他版本。版本可能包含不兼容的更改,例如包含一个与代码中标识符冲突的新关键字。但是,除非你选择这些更改,否则即使你升级了所使用的 Rust 编译器版本,你的代码也将继续编译。
Each project can opt in to an edition other than the default 2015 edition. Editions can contain incompatible changes, such as including a new keyword that conflicts with identifiers in code. However, unless you opt in to those changes, your code will continue to compile even as you upgrade the Rust compiler version you use.
所有 Rust 编译器版本都支持在该编译器发布之前存在的任何版本,并且它们可以将任何受支持版本的 crates 链接在一起。版本的更改仅影响编译器初始解析代码的方式。因此,如果你使用的是 Rust 2015 且你的依赖项之一使用的是 Rust 2018,你的项目将能够编译并使用该依赖项。相反的情况(你的项目使用 Rust 2018 且依赖项使用 Rust 2015)同样有效。
All Rust compiler versions support any edition that existed prior to that compiler’s release, and they can link crates of any supported editions together. Edition changes only affect the way the compiler initially parses code. Therefore, if you’re using Rust 2015 and one of your dependencies uses Rust 2018, your project will compile and be able to use that dependency. The opposite situation, where your project uses Rust 2018 and a dependency uses Rust 2015, works as well.
明确一点:大多数功能在所有版本上都可用。使用任何 Rust 版本的开发人员都将随着新的稳定版本的发布而继续看到改进。但是,在某些情况下(主要是添加新关键字时),某些新功能可能仅在以后的版本中可用。如果你想利用这些功能,则需要切换版本。
To be clear: Most features will be available on all editions. Developers using any Rust edition will continue to see improvements as new stable releases are made. However, in some cases, mainly when new keywords are added, some new features might only be available in later editions. You will need to switch editions if you want to take advantage of such features.
有关更多详细信息,请参阅《Rust 版本指南》(The Rust Edition Guide)edition-guide。这是一本完整的书,列举了版本之间的差异,并解释了如何通过 cargo fix 自动将代码升级到新版本。
For more details, see The Rust Edition Guide. This is a
complete book that enumerates the differences between editions and explains how
to automatically upgrade your code to a new edition via cargo fix.