|
|
|
@ -0,0 +1,69 @@
|
|
|
|
|
# Rust 新版解读 | 1.84 | Const 能力拓展
|
|
|
|
|
|
|
|
|
|
> Rust 1.84 官方 release doc: [Announcing Rust 1.84.0 | Rust Blog](https://blog.rust-lang.org/2025/01/09/Rust-1.84.0.html)
|
|
|
|
|
|
|
|
|
|
通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.84 版本:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
$ rustup update stable
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Cargo 在依赖版本选择中考虑 Rust 版本
|
|
|
|
|
|
|
|
|
|
1.84.0 稳定了支持最低 Rust 版本(MSRV, minimum supported Rust version)的解析器,该解析器优先选择与项目声明的 MSRV (`Cargo.toml` 里的 `package.rust_version`) 兼容的依赖版本。通过支持 MSRV 的版本选择,维护者可以减少支持旧工具链的工作量,因为不再需要为每个依赖手动选择旧版本。
|
|
|
|
|
|
|
|
|
|
你可以通过 `.cargo/config.toml` 启用支持 MSRV 的解析器:
|
|
|
|
|
|
|
|
|
|
```toml
|
|
|
|
|
[resolver]
|
|
|
|
|
incompatible-rust-versions = "fallback"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然后在添加依赖时:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ cargo add clap
|
|
|
|
|
Updating crates.io index
|
|
|
|
|
warning: ignoring clap@4.5.23 (which requires rustc 1.74) to maintain demo's rust-version of 1.60
|
|
|
|
|
Adding clap v4.0.32 to dependencies
|
|
|
|
|
Updating crates.io index
|
|
|
|
|
Locking 33 packages to latest Rust 1.60 compatible versions
|
|
|
|
|
Adding clap v4.0.32 (available: v4.5.23, requires Rust 1.74)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在 CI 中[验证最新依赖](https://doc.rust-lang.org/cargo/guide/continuous-integration.html#verifying-latest-dependencies)时,你可以覆盖此行为:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=allow cargo update
|
|
|
|
|
Updating crates.io index
|
|
|
|
|
Locking 12 packages to latest compatible versions
|
|
|
|
|
Updating clap v4.0.32 -> v4.5.23
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
你也可以通过在 `Cargo.toml` 清单文件中设置 [`package.resolver = "3"`](https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions) 来启用此功能,但这需要将 MSRV 提升到 1.84。对于使用 Rust 2024 的项目(将在 1.85 中稳定),新的解析器将默认启用。
|
|
|
|
|
|
|
|
|
|
这为库作者在决定采用新 Rust 工具链功能的策略时提供了更大的灵活性。以前,库采用新 Rust 工具链的功能会迫使使用旧 Rust 版本的下游用户要么升级工具链,要么手动选择与工具链兼容的旧版本库(并避免运行 `cargo update`)。现在,这些用户将能够自动使用与其旧工具链兼容的旧库版本。
|
|
|
|
|
|
|
|
|
|
有关决定 MSRV 策略时的更多注意事项,请参阅[文档](https://doc.rust-lang.org/cargo/reference/rust-version.html#setting-and-updating-rust-version)。
|
|
|
|
|
|
|
|
|
|
## 新 trait 求解器的迁移开始
|
|
|
|
|
|
|
|
|
|
Rust 编译器正在迁移到新的 trait 求解器实现。下一代 trait 求解器是 Rust 类型系统核心组件的重新实现。它不仅负责检查 trait 边界(例如 `Vec<T>: Clone`)是否成立,还被类型系统的许多其他部分使用,例如规范化(确定 `<Vec<T> as IntoIterator>::Item` 的基础类型)和类型等价(检查 `T` 和 `U` 是否相同)。
|
|
|
|
|
|
|
|
|
|
在 1.84 中,新求解器用于检查 trait 实现的一致性。从高层次来看,一致性负责确保在考虑其他 crate 中尚未编写或不可见的代码时,给定类型的 trait 实现最多只有一个。
|
|
|
|
|
|
|
|
|
|
此稳定化修复了旧实现中的一些主要理论上的正确性问题,导致可能会报告以前未报告的“trait 冲突实现”错误。根据 [Crater](https://github.com/rust-lang/crater/) 对可用代码的评估,我们预计受影响的模式非常罕见。此次稳定版本还提高了我们证明实现不重叠的能力,在某些情况下允许编写更多代码。
|
|
|
|
|
|
|
|
|
|
有关更多详细信息,请参阅之前的[博客文章](https://blog.rust-lang.org/inside-rust/2024/12/04/trait-system-refactor-initiative.html)和[稳定报告](https://github.com/rust-lang/rust/pull/130654)。
|
|
|
|
|
|
|
|
|
|
## 严格来源 API
|
|
|
|
|
|
|
|
|
|
在 Rust 中,[指针不仅仅是“整数”或“地址”](https://rust-lang.github.io/rfcs/3559-rust-has-provenance.html)。例如,“释放后使用”是未定义行为,即使你“幸运”并且在读取/写入之前重新分配了释放的内存。另一个例子是,通过从 `&i32` 引用派生的指针写入是未定义行为,即使通过不同指针写入同一地址是合法的。这里的底层模式是,指针的计算方式很重要,而不仅仅是计算结果的地址。因此,我们说指针具有**来源**:要完全描述 Rust 中与指针相关的未定义行为,我们不仅需要知道指针指向的地址,还需要跟踪它是从哪些其他指针“派生”的。
|
|
|
|
|
|
|
|
|
|
大多数情况下,程序员不需要过多担心来源,指针的派生方式非常清晰。然而,当将指针转换为整数并返回时,结果指针的来源是不明确的。在此版本中,Rust 添加了一组 API,可以在许多情况下替代整数指针转换的使用,从而避免此类转换固有的歧义。特别是,现在可以在不将指针转换为整数(或反方向将整数转换成指针)的情况下实现使用对齐指针的最低有效位存储额外信息的模式。这使得代码更易于推理,更易于编译器分析,并且还有益于像 [Miri](https://github.com/rust-lang/miri) 这样的工具和像 [CHERI](https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/) 这样的架构,旨在检测和诊断指针滥用。
|
|
|
|
|
|
|
|
|
|
有关更多详细信息,请参阅标准库中关于[来源](https://doc.rust-lang.org/std/ptr/index.html#provenance)的文档。
|
|
|
|
|
|
|
|
|
|
## Others
|
|
|
|
|
|
|
|
|
|
其它更新细节,和稳定的 API 列表,包括上述 `provenance` 相关 API,参考[原Blog](https://blog.rust-lang.org/2025/01/09/Rust-1.84.0.html#stabilized-apis)
|