Rust 1.98
最後更新
Rust 1.98 發布於 2026-08-20,目前最新修補版為
1.98.1,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.98 為
f32/f64 加上「代數」運算方法(algebraic_add 等),允許編譯器套用實數的代數性質重排運算以換取效能,效果類似其他語言的 -ffast-math。整數新增 format_into,把數字格式化到堆疊上的 NumBuffer,效能與熱門的 itoa crate 相當。升級時要注意:同時 derive PartialOrd 與 Ord 時改走快速路徑,兩者實作不一致的型別可能出錯;std::env::Vars 不再實作 Send/Sync;部分歧義匯入從 lint 變成錯誤。語言變更
- 在 unsize 強制轉型時,即使在不變(invariant)位置也允許縮短
&mut的生命週期。 - 新增預設拒絕的
invalid_runtime_symbol_definitions與預設警告的suspicious_runtime_symbol_definitionslint,針對自行定義memcmp、memset等執行期符號。 - 新增預設警告的
c_void_returnslint,檢查以core::ffi::c_void當回傳型別,詳見 FFI。
標準庫重點
f32/f64新增algebraic_add、algebraic_sub、algebraic_mul、algebraic_div、algebraic_rem,詳見 f64。core::fmt::NumBuffer與整數的format_into,詳見 std::fmt。bool::ok_or、bool::ok_or_else,以及Option::map_or_default、Result::map_or_default,詳見 Option 與 Result。str::strip_circumfix、[T]::strip_circumfix同時去掉開頭與結尾;str::substr_range、[T]::subslice_range找出從原字串切出的子字串所在的位元組範圍,詳見 str。String::from_utf16le、from_utf16be及其_lossy版本,詳見 String。NonZero::from_str_radix、Box::as_ptr、Path::is_empty、PathBuf::into_string、std::range::legacy與Atomic<T>的from_mut系列。derive巨集可經由{core,std}::derive使用(最低版本以 1.96 計);PanicHookInfo中的Location生命週期改為'static。- 文件明確保證:對已 drop 的
ManuallyDrop<Box<_>>進行移動不是未定義行為,詳見 std::mem。
Cargo 與工具
- rustfmt 會找到在
cfg_select!中定義的模組檔案,可能因此格式化到以前被忽略的程式碼,詳見開發工具。
相容性注意
- 同時 derive
Ord時,derive(PartialOrd)改用快速路徑,PartialOrd與Ord不一致的型別可能出錯,詳見 std::cmp。 std::env::Vars、VarsOs不再實作Send/Sync,詳見 std::env。repr(transparent)對「可忽略欄位」更嚴格:repr(C)型別、有私有欄位的型別與#[non_exhaustive]型別不再算數。assert_eq!、assert_ne!加入暫存值作用範圍;印出字串與字元時會跳脫更多字元。- 部分
ambiguous_glob_imports情況改為錯誤;Type = Type形式的 where 條件在語法上不再允許。 - Solaris 移除
File::lock實作,改回傳 unsupported。
代數浮點運算
浮點數加法不滿足結合律:
a + b + c + d 必須照 ((a + b) + c) + d 的順序計算,編譯器不能自行重排,也就難以平行化或向量化。改用 algebraic_add 等方法串接時,編譯器可以依實數的代數性質重排,例如先算 (a + b) 與 (c + d)。範例的每個中間結果都是可以精確表示的整數,所以不論怎麼重排結果都一樣。範例:
- fn sum_strict(xs: &[f64]) -> f64 {
- xs.iter().fold(0.0, |acc, x| acc + x)
- }
- fn sum_algebraic(xs: &[f64]) -> f64 {
- xs.iter().fold(0.0, |acc, x| acc.algebraic_add(*x))
- }
- fn main() {
- let xs: Vec<f64> = (1..=100).map(f64::from).collect();
- println!("{}", sum_strict(&xs));
- println!("{}", sum_algebraic(&xs));
- println!("{}", 6.0f64.algebraic_mul(7.0));
- }
輸出:
- 5050
- 5050
- 42
注意:這些方法的結果是非確定性的,編譯器選擇的最佳化不同,數值可能有細微差異;但它們不會造成未定義行為。需要逐位元可重現的結果時,請使用一般的
+、*。format_into 與 NumBuffer
n.to_string() 每次都會配置一個新的 String。format_into 把十進位結果寫進呼叫端提供的 NumBuffer(大小足以容納該型別任何值),回傳借用自緩衝區的 &str,不需要堆積配置,也省去 write! 的動態分派。NumBuffer 在 1.98 位於 core::fmt,1.99 起也能從 std::fmt 取得。範例:
- use core::fmt::NumBuffer;
- fn main() {
- let mut buf = NumBuffer::new();
- let s: &str = 12345u32.format_into(&mut buf);
- println!("{s}({} 個位元組)", s.len());
- let mut out = String::new();
- for n in [-7i64, 0, i64::MAX] {
- let mut buf = NumBuffer::new();
- out.push_str(n.format_into(&mut buf));
- out.push(' ');
- }
- println!("{}", out.trim_end());
- }
輸出:
- 12345(5 個位元組)
- -7 0 9223372036854775807
注意:回傳的
&str 借用自緩衝區,緩衝區被重新使用或離開作用域後就不能再用。本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.98.1 | 2026-09-03 | 否 | rustc: fix miscompilation in generating vtables |
已知漏洞
影響
1.98.0 的已知漏洞共 0 個;在本版修正的共 0 個;仍影響最新修補版 1.98.1 的共 0 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。