Rust 1.100
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
The Rust team is happy to announce a new version of Rust, 1.100.0. Rust is a programming language empowering everyone to build reliable and efficient software.
Rust 团队很高兴地宣布 Rust 的新版本 1.100.0。Rust 是一门旨在让每个人都能构建可靠且高效软件的编程语言。
If you have a previous version of Rust installed via rustup, you can get 1.100.0 with:
如果您已经通过 rustup 安装了旧版本的 Rust,可以通过以下命令获取 1.100.0:
If you don’t have it already, you can get rustup from the appropriate page on our website, and check out the detailed release notes for 1.100.0.
如果您还没有安装,可以从我们网站的相关页面获取 rustup,并查看 1.100.0 的详细发布说明。
If you’d like to help us out by testing future releases, you might consider updating locally to use the beta channel (rustup default beta) or the nightly channel (rustup default nightly). Please report any bugs you might come across!
如果您想通过测试未来版本来帮助我们,可以考虑在本地更新并使用 beta 通道(rustup default beta)或 nightly 通道(rustup default nightly)。请报告您遇到的任何错误!
After 10 years and 5 failed stabilization attempts, the never type is finally stable! This also completes the project goal for this work.
经过 10 年和 5 次失败的稳定化尝试,“never”类型终于稳定了!这也完成了该项工作的项目目标。
The never type, spelled ! in code, is the canonical uninhabited type. It represents the type of diverging computations – computations which never resolve to any value – where any code that follows is unreachable. Expressions that terminate control flow, such as return, break, and std::process::exit, also have the type !.
“never”类型(在代码中写作 !)是规范的空类型(uninhabited type)。它代表发散计算的类型——即永远不会解析为任何值的计算——其后的任何代码都是不可达的。终止控制流的表达式,如 return、break 和 std::process::exit,其类型也为 !。
Since having a value of the never type is impossible, Rust permits a never-to-any coercion, allowing to turn ! into any other type (this is what distinguishes it from any other uninhabited types). This can be used, for example, to not return a value from one of the branches of a match or an if:
由于不可能拥有“never”类型的值,Rust 允许将 ! 强制转换为任何其他类型(这使其区别于其他任何空类型)。例如,这可以用于在 match 或 if 的某个分支中不返回任何值:
While the never type could already be used and observed this way, and even named in the return type of functions (-> !), this release allows naming the never type anywhere:
虽然“never”类型以前就可以通过这种方式使用和观察,甚至可以在函数的返回类型(-> !)中命名,但此版本允许在任何地方命名“never”类型:
Additionally this release changes Infallible to be an alias to the never type. That makes all old APIs that used Infallible be compatible with using !. This was the plan ever since the introduction of Infallible and is one of the reasons why the never type stabilization was so hard (waffle gave a talk about some of the troubles of the stabilization).
此外,此版本将 Infallible 更改为“never”类型的别名。这使得所有使用 Infallible 的旧 API 都能与 ! 兼容。这是自引入 Infallible 以来的既定计划,也是“never”类型稳定化如此困难的原因之一(waffle 曾就稳定化过程中的一些困难发表过演讲)。
Unfortunately, to allow changing Infallible, we had to introduce a breaking change. This release changes the “never type fallback” from () to ! in editions before 2024 (i.e. 2015, 2018, and 2021), similarly to what has already been done in edition 2024. This should largely be unnoticeable, but can affect code in some rare cases (for example when ignoring a result of a function returning a generic parameter). Note that this change might also make the compiler detect more code as unreachable.
遗憾的是,为了允许更改 Infallible,我们不得不引入一个破坏性变更。此版本将 2024 版之前的版本(即 2015、2018 和 2021 版)中的“never 类型回退”从 () 更改为 !,这与 2024 版中已经完成的操作类似。这在很大程度上应该是不可察觉的,但在极少数情况下可能会影响代码(例如在忽略返回泛型参数的函数结果时)。请注意,此更改也可能导致编译器检测到更多不可达代码。
We tried to minimize the impact of this breaking change as much as possible, by providing future compatibility warnings far in advance and releasing fixes to public crates that have been broken by this. If a dependency is failing due to issues with the never type, consider updating it, as a fix might have already been released for it.
我们通过提前提供未来兼容性警告,并为受此影响的公共 crate 发布修复程序,尽可能地减少了此次破坏性变更的影响。如果某个依赖项因“never”类型的问题而失败,请考虑更新它,因为可能已经有针对该问题的修复程序发布。
This release brings about the stabilization of the core parts of allocator_api, enabling library authors to define custom allocators that can be used together with certain standard library types - currently Vec and Box. These are enabled through the newly-stabilized Allocator trait.
此版本实现了 allocator_api 核心部分的稳定化,使库作者能够定义可与某些标准库类型(目前为 Vec 和 Box)一起使用的自定义分配器。这些功能通过新稳定的 Allocator trait 启用。
This is the foundation of the Allocators 1.0 project goal. A lot remains to be done still, including expanding the API surface usable with custom allocators and stabilizing their use on more collections and pointer types; with the core trait and design out of the way though, expect to see more being stabilized soon!
这是“分配器 1.0”项目目标的基础。仍有许多工作要做,包括扩展可与自定义分配器一起使用的 API 表面,并稳定它们在更多集合和指针类型上的使用;不过,随着核心 trait 和设计的完成,预计很快会有更多功能稳定下来!
The Drop trait in Rust makes it possible to run code when a variable goes out of scope, regardless of whether we return, ?, or panic. Drop being a part of types means that in Rust you never have to close a socket.
Rust 中的 Drop trait 使得在变量超出作用域时运行代码成为可能,无论我们是返回、使用 ? 还是发生 panic。Drop 作为类型的一部分意味着在 Rust 中,您永远不必手动关闭套接字。
Implementing Drop can however feel a little inconvenient when all you want is to run some code unconditionally when a function exits. This is why we’re introducing core::mem::DropGuard: a new helper type that runs a closure when dropped.
然而,当您只想在函数退出时无条件运行某些代码时,实现 Drop 可能会感到有些不便。这就是我们引入 core::mem::DropGuard 的原因:这是一种新的辅助类型,可以在被丢弃时运行闭包。
DropGuard takes two arguments: a variable and a closure. It implements Deref and DerefMut so you can keep working with the variable as normal. But when dropped it runs the closure and consumes the variable.
DropGuard 接受两个参数:一个变量和一个闭包。它实现了 Deref 和 DerefMut,因此您可以像往常一样继续使用该变量。但在被丢弃时,它会运行闭包并消耗该变量。
DropGuard also implements a dismiss method which returns the variable from the guard without running the destructor. This makes it possible to quickly implement all-or-nothing transaction-like APIs with a rollback. Here is an example implementing a parser which uses a guard to roll back mutations in case of failure:
DropGuard 还实现了一个 dismiss 方法,该方法可以从 guard 中返回变量而不运行析构函数。这使得快速实现具有回滚功能的“全有或全无”事务式 API 成为可能。以下是一个实现解析器的示例,它使用 guard 在失败时回滚变更:
As Rust’s unsafe code guidelines are still being decided, it became clear that it would be very useful to decide what exactly it means to create zero-sized types in an unsafe way, and what we should recommend for people to do this.
由于 Rust 的不安全代码指南仍在制定中,很明显,明确以不安全方式创建零大小类型(ZST)的确切含义,以及我们应该建议人们如何执行此操作,将非常有用。
This question is answered in Rust 1.100 with a single function: std::mem::conjure_zst.
Rust 1.100 通过一个函数回答了这个问题:std::mem::conjure_zst。
Instead of transmuting (), MaybeUninit::uninit().assume_init(), or various other incantations, this is the dedicated way to create a value of a zero-sized type when you know that doing so is safe to do. If an inhabited type has no size, we know exactly what value it must be: nothing.
与其使用 transmute ()、MaybeUninit::uninit().assume_init() 或其他各种咒语,这是当您知道这样做是安全时,创建零大小类型值的专用方法。如果一个非空类型没有大小,我们确切地知道它必须是什么值:什么都没有。
Of course, APIs often rely on the inability to construct such types for safety, and that’s why this function is unsafe. For example, PyO3’s Python<‘py> is a zero-sized marker token that represents holding the Python GIL (global interpreter lock). Many of Python’s APIs require holding this lock to be safe, but conjuring a token from thin air would allow safe PyO3 calls to violate that precondition.
当然,API 通常依赖于无法构造此类类型来保证安全,这就是该函数不安全的原因。例如,PyO3 的 Python<'py> 是一个零大小的标记令牌,代表持有 Python GIL(全局解释器锁)。许多 Python 的 API 需要持有此锁才能安全运行,但凭空变出一个令牌将允许安全代码调用违反该前提条件。
Building dependencies that are no longer used or are only needed for specific platforms can slow down builds. Rustc has the unused_crate_dependencies lint for this but it has false positives when used with Cargo because dependencies may be shared between multiple build targets, e.g. dependencies.tracing-subscriber being available for every bin in a package when only some may use it.
构建不再使用或仅在特定平台上需要的依赖项会拖慢构建速度。Rustc 为此提供了 unused_crate_dependencies lint,但在与 Cargo 一起使用时会出现误报,因为依赖项可能在多个构建目标之间共享,例如 dependencies.tracing-subscriber 对包中的每个二进制文件都可用,而实际上只有部分文件可能使用它。
Cargo now reports unused dependencies to users, e.g.
Cargo 现在会向用户报告未使用的依赖项,例如:
A limitation of this warning is that it will only appear when building every build target that may use an entry in a dependencies table. For example, running cargo check —bin foo won’t show warnings for unused [
此警告的一个局限性是,它仅在构建可能使用依赖项表中条目的每个构建目标时才会出现。例如,运行 cargo check --bin foo 不会显示未使用的 [ 的警告。