translation(appendix/rust-versions) : 1.81

pull/1465/head
EluvK 2 weeks ago
parent 0e69758bd8
commit 8f27697436

@ -387,3 +387,4 @@
- [1.78](appendix/rust-versions/1.78.md)
- [1.79](appendix/rust-versions/1.79.md)
- [1.80](appendix/rust-versions/1.80.md)
- [1.81](appendix/rust-versions/1.81.md)

@ -0,0 +1,54 @@
# Rust 新版解读 | 1.81 | `expect` lint
> Rust 1.81 官方 release doc: [Announcing Rust 1.81.0 | Rust Blog](https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html)
通过 [rustup](https://www.rust-lang.org/tools/install) 安装的同学可以使用以下命令升级到 1.81 版本:
```shell
$ rustup update stable
```
## `core::error::Error`
1.81 版本稳定了 `core::error::Error` trait允许在 core 中使用 Error trait使得库可以在 #![no_std] 环境中使用。这主要是为了让 Rust 生态系统能够在不同的环境中标准化相同的 Error trait无论库的目标环境是什么。
## 新的排序实现
标准库中的稳定和不稳定排序实现都已更新为新算法,提高了运行时性能和编译时间。
此外,新的排序算法都会尝试检测 `Ord` 的不正确实现,这些实现会阻止它们能够产生有意义的排序结果,并且现在会在这些情况下 panic而不是返回实际上是随机排列的数据。遇到这些 panic 的用户应该审查他们的排序实现,以确保它们满足 [`PartialOrd`](https://doc.rust-lang.org/nightly/std/cmp/trait.PartialOrd.html) 和 [`Ord`](https://doc.rust-lang.org/nightly/std/cmp/trait.Ord.html) 的要求。
## `#[expect(lint)]`
1.81 版本引入了一个新的 lint 级别 `expect`,允许显式地指定一个特定的 lint **应该发生**,并在没有发生时发出警告。这个功能的预期用例是暂时消除一个 lint无论是由于 lint 实现错误还是正在进行的重构,同时希望在不再需要 lint 时得到通知。
例如,如果您正在移动代码库以符合通过 Clippy lint 强制执行的新限制,比如 [`undocumented_unsafe_blocks`](https://rust-lang.github.io/rust-clippy/stable/index.html#/undocumented_unsafe_blocks),您可以在过渡期间使用 `#[expect(clippy::undocumented_unsafe_blocks)]`,确保一旦所有的 unsafe 块都被文档化,您可以选择拒绝 lint 以强制执行它。
Clippy 还有两个 lint 来强制使用这个特性,并帮助迁移现有的属性:
[`clippy::allow_attributes`](https://rust-lang.github.io/rust-clippy/master/index.html#/allow_attributes) 限制 `#[allow]` 属性,鼓励使用 `#[expect]` 或将 `#[allow]` 属性迁移到 `#[expect]`
[`clippy::allow_attributes_without_reason`](https://rust-lang.github.io/rust-clippy/master/index.html#/allow_attributes_without_reason) 要求为 `#[allow]` 属性提供原因
## `Lint reasons`
更改 lint 级别通常是出于某种特定原因。例如,如果代码在没有浮点支持的环境中运行,您可以使用 Clippy 如 `#![deny(clippy::float_arithmetic)]` 来阻止此类使用。然而,如果项目中的新开发人员看到这个 lint他们可能需要在 deny 的注释里(如果有的话)寻找原因。从 Rust 1.81 开始,他们可以直接在编译器消息中得到通知:
```bash
error: floating-point arithmetic detected
--> src/lib.rs:4:5
|
4 | a + b
| ^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#float_arithmetic
= note: no hardware float support
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::float_arithmetic, reason = "no hardware float support")]
| ^^^^^^^^^^^^^^^^^^^^^^^^
```
## Others
其它更新细节,和稳定的 API 列表,参考[原Blog](https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#stabilized-apis)
Loading…
Cancel
Save