Rust 1.98.0 发布
摘要
Rust 1.98.0 发布,引入代数浮点方法以优化操作,缓冲整数格式化以提高性能,并修复了 ManuallyDrop 和 Box 之间的交互问题。
<p><a href="https://lobste.rs/s/hbjeir/announcing_rust_1_98_0">评论</a></p>
查看缓存全文
缓存时间: 2026/08/21 02:50
# 发布 Rust 1.98.0 | Rust 博客
来源:https://blog.rust-lang.org/2026/08/20/Rust-1.98.0/
Rust 团队高兴地宣布 Rust 新版本 1.98.0 的发布。Rust 是一门让每个人都能构建可靠且高效软件的编程语言。
如果你已通过 `rustup` 安装了之前版本的 Rust,可以使用以下命令获取 1.98.0 版本:
```
$ rustup update stable
```
若尚未安装,可以从我们网站的相关页面获取 `rustup` (https://www.rust-lang.org/install.html),并查阅 1.98.0 的详细发行说明 (https://doc.rust-lang.org/stable/releases.html#version-1980-2026-08-20)。
如果你想通过测试未来版本来帮助我们,可以考虑本地更新至测试版通道 (`rustup default beta`) 或每夜构建通道 (`rustup default nightly`)。
请通过此链接 (https://github.com/rust-lang/rust/issues/new/choose) 报告你遇到的任何错误!
## 1.98.0 稳定版中的新特性
### 代数浮点数方法
浮点类型 `f32` 和 `f64` 现在新增了用于加法、减法、乘法、除法和取余的“代数”方法。这些方法允许利用实数代数性质进行运算优化,即使这些性质在浮点表示的限制下并不总成立。具体优化细节未严格定义,但可能类似于其他语言中 `-ffast-math` 选项的效果。
例如,浮点加法不满足结合律 (https://en.wikipedia.org/wiki/Associative_property#Nonassociativity_of_floating-point_calculation),因此像 `a + b + c + d` 这样的求和必须按解析的左结合顺序 `((a + b) + c) + d` 计算。若改用 `algebraic_add` 调用链编写相同的求和,编译器可自由重排运算顺序,例如采用 `(a + b) + (c + d)` 同时计算部分和。这些代数方法通常也能支持更广泛的循环向量化优化。
这些方法的结果是非确定的,因为编译器可选择不同的优化路径,但绝不会引发未定义行为。详见库文档 (https://doc.rust-lang.org/stable/core/primitive.f32.html#algebraic-operators) 和原始 API 变更提案 (https://github.com/rust-lang/libs-team/issues/532)。
### 缓冲整数格式化
所有原始整数类型现在都提供了 `format_into` 方法 (https://doc.rust-lang.org/stable/core/primitive.usize.html#method.format_into),该方法接受一个 `&mut NumBuffer` 参数。该缓冲区大小足以存储对应类型任意值的十进制表示。缓冲区本身是不透明的,但方法会返回借用该缓冲区生命周期的格式化字符串 `&str`。
此方法还能避免使用缓冲式 `write!` 格式化时常见的动态分发开销,从而提升性能。`itoa-benchmark` (https://github.com/dtolnay/itoa-benchmark) 仓库显示 `format_into` 的性能与 `itoa` 本身相当,因此它可作为此类依赖项的标准替代方案。
### 修复 `ManuallyDrop` 与 `Box` 的交互问题
在 Rust 1.96.0 之前,编译器存在一个缺陷,导致以下代码属于未定义行为:
```
let mut x = ManuallyDrop::new(Box::new(1));
unsafe { ManuallyDrop::drop(&mut x) }
let x = x; // 未定义行为!
```
这是因为编译器将移动已释放(析构)的 `Box` 视为未定义行为,而 `ManuallyDrop` 原先会传播此规则,导致移动已析构的 `ManuallyDrop<Box>` 也被视为未定义行为。在 Rust 1.96.0 中已修复此问题,使该代码不再属于未定义行为。
本次更新完善了 `ManuallyDrop` 文档,明确保证此类代码在未来仍不会产生未定义行为。详情请查阅 `ManuallyDrop` 文档 (https://doc.rust-lang.org/stable/std/mem/struct.ManuallyDrop.html#pre-196-interaction-with-box) 及相关 RFC 3336 (https://rust-lang.github.io/rfcs/3336-maybe-dangling.html)。
### 稳定 API
- `str::substr_range` (https://doc.rust-lang.org/stable/std/primitive.str.html#method.substr_range)
- [`[T]::subslice_range`](https://doc.rust-lang.org/stable/std/primitive.slice.html#method.subslice_range)
- `core::fmt::NumBuffer` (https://doc.rust-lang.org/stable/core/fmt/struct.NumBuffer.html)
- `<{integer}>::format_into` (https://doc.rust-lang.org/stable/core/primitive.usize.html#method.format_into)
- `Send/Sync for std::process::CommandArgs` (https://doc.rust-lang.org/stable/std/process/struct.CommandArgs.html#impl-Send-for-CommandArgs%3C'a%3E)
- `{fN}::algebraic_add` (https://doc.rust-lang.org/stable/core/primitive.f32.html#method.algebraic_add)
- `{fN}::algebraic_sub` (https://doc.rust-lang.org/stable/core/primitive.f32.html#method.algebraic_sub)
- `{fN}::algebraic_mul` (https://doc.rust-lang.org/stable/core/primitive.f32.html#method.algebraic_mul)
- `{fN}::algebraic_div` (https://doc.rust-lang.org/stable/core/primitive.f32.html#method.algebraic_div)
- `{fN}::algebraic_rem` (https://doc.rust-lang.org/stable/core/primitive.f32.html#method.algebraic_rem)
- `NonZero<{integer}>::from_str_radix` (https://doc.rust-lang.org/stable/core/num/struct.NonZero.html#method.from_str_radix-4)
- `String::from_utf16le` (https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf16le)
- `String::from_utf16le_lossy` (https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf16le_lossy)
- `String::from_utf16be` (https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf16be)
- `String::from_utf16be_lossy` (https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf16be_lossy)
- [`[T]::strip_circumfix`](https://doc.rust-lang.org/stable/core/primitive.slice.html#method.strip_circumfix)
- `str::strip_circumfix` (https://doc.rust-lang.org/stable/core/primitive.str.html#method.strip_circumfix)
- `Atomic::from_mut` (https://doc.rust-lang.org/stable/core/sync/atomic/struct.Atomic.html#method.from_mut)
- `Atomic::get_mut_slice` (https://doc.rust-lang.org/stable/core/sync/atomic/struct.Atomic.html#method.get_mut_slice)
- `Atomic::from_mut_slice` (https://doc.rust-lang.org/stable/core/sync/atomic/struct.Atomic.html#method.from_mut_slice)
- `std::range::legacy` (https://doc.rust-lang.org/stable/std/range/legacy/index.html)
### 其他变更
请查看 Rust (https://github.com/rust-lang/rust/releases/tag/1.98.0)、Cargo (https://doc.rust-lang.org/nightly/cargo/CHANGELOG.html#cargo-198-2026-08-20) 和 Clippy (https://github.com/rust-lang/rust-clippy/blob/master/CHANGELOG.md#rust-198) 中的所有变更。
## 1.98.0 的贡献者
众多贡献者共同创造了 Rust 1.98.0。感谢你们所有人!(https://thanks.rust-lang.org/rust/1.98.0/)
相似文章
Rust 1.99.0 发布公告
Rust 团队宣布推出 Rust 1.99.0,稳定了 C-ABI 可变参数函数、基于原始指针的布局信息 API,并在自定义分配器支持到来之前更新了关于 Box::leak 用法的指导建议。
Rust 1.98.1 发布
Rust 1.98.1 是一个补丁版本,修复了 vtable 生成中的一个错误编译漏洞,该漏洞可能导致编译后的代码出现未定义行为。
借助 Rust 新 API 实现更快的浮点运算
Rust 1.98 引入了一个新 API,通过允许更激进的编译器优化来加速浮点运算,同时仍让开发者控制舍入误差。
Rust 1.97.0 发布公告
Rust 1.97.0 已发布,默认启用符号修饰 v0,Cargo 支持禁止警告,并且链接器输出不再隐藏。
宣布发布 Rust 1.99.1
Rust 1.99.1 发布,这是一个修复性点版本,解决了 wcslen 符号缺失、x86_64 上 FFI bool 返回值误编译以及 macOS 上 rustlld 无法加载 libLLVM 三项回归问题。