Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

附录 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 用户,新版本将增量变化整合到一个易于理解的包中。

  • 对于非用户,新版本标志着一些重大进展已经落地,这可能值得再次关注 Rust。

  • 对于 Rust 的开发者,新版本为整个项目提供了一个凝聚点。

  • For active Rust users, a new edition brings together incremental changes into an easy-to-understand package.

  • For non-users, a new edition signals that some major advancements have landed, which might make Rust worth another look.

  • 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 编译器版本都支持该编译器发布之前存在的任何版本,并且它们可以将任何受支持版本的 crate 链接在一起。版本的更改仅影响编译器最初解析代码的方式。因此,如果你使用的是 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 版本指南。这是一本完整的书,列举了版本之间的差异,并解释了如何通过 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.