Rust 1.97
最後更新
Rust 1.97 發布於 2026-07-09,目前最新修補版為
1.97.1,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.97 在穩定版預設改用 v0 符號修飾(symbol mangling)方案,泛型參數的實際值會保留在符號名稱中,不再只藏在雜湊後面。Cargo 新增
build.warnings 設定,統一控制警告要忽略、顯示或視為錯誤,取代在 CI 設 RUSTFLAGS=-Dwarnings 的做法。升級時要注意:舊版的除錯器或效能分析工具可能無法解讀 v0 符號,backtrace 的文字格式也可能改變;連結器的輸出預設會以警告顯示;std::char 模組的常數與函式被標記為棄用。語言變更
must_uselint 把Result<T, 無值型別>與ControlFlow<無值型別, T>視同T。- 新增預設關閉的
dead_code_pub_in_binarylint,找出 binary crate 中沒用到的pub項目。 - 穩定
cfg(target_has_atomic_primitive_alignment)。 - 更多情況允許在匯入路徑結尾寫
self,詳見模組系統。
標準庫重點
- 整數與
NonZero整數新增bit_width、highest_one、lowest_one、isolate_highest_one、isolate_lowest_one,詳見數值型別。 RepeatN實作Default,ffi::FromBytesUntilNulError實作Copy。char::is_control可在 const 情境使用,詳見 char。
Cargo 與工具
- 穩定
build.warnings設定:allow隱藏警告、warn(預設)顯示、deny讓建置失敗;改用這個設定不會讓建置快取失效,詳見 Cargo。 - 穩定
resolver.lockfile-path,指定解析相依時使用的 lockfile 路徑。 cargo clean在--target-dir看起來不像 Cargo 目標目錄時報錯,避免誤刪。- 新增
-m作為--manifest-path的縮寫。 - rustdoc 穩定
--emit與--remap-path-prefix。
相容性注意
- 預設使用 v0 符號修飾,舊版工具可能無法還原符號名稱。
- 連結器輸出預設以
linker_messages警告顯示,可在Cargo.toml的[lints.rust]設為allow。 std::char模組的常數與函式標記為棄用,詳見棄用 API。pin!不再做解參考強制轉型:pin!(x)在x是&mut T時一律得到Pin<&mut &mut T>,詳見 std::pin。- 移除自 1.0 起就已棄用的隱藏
f64方法。 - Windows 上對已
shutdown寫入端的 socket 寫入,錯誤種類改為BrokenPipe,詳見 std::net。
整數的位元查詢方法
處理旗標、位元遮罩或計算需要幾個位元時,常要自己組合
leading_zeros 與位移。新方法直接給答案:bit_width 是表示該數值需要的位元數;highest_one、lowest_one 回傳最高、最低的 1 所在的位元位置(數值為 0 時是 None);isolate_highest_one、isolate_lowest_one 只保留最高或最低的那個 1。範例:
- fn main() {
- for n in [0u32, 1, 12, 40, 255] {
- println!(
- "{n:>3} = {n:08b}:bit_width {},highest {:?},lowest {:?},isolate_highest {},isolate_lowest {}",
- n.bit_width(),
- n.highest_one(),
- n.lowest_one(),
- n.isolate_highest_one(),
- n.isolate_lowest_one(),
- );
- }
- }
輸出:
- 0 = 00000000:bit_width 0,highest None,lowest None,isolate_highest 0,isolate_lowest 0
- 1 = 00000001:bit_width 1,highest Some(0),lowest Some(0),isolate_highest 1,isolate_lowest 1
- 12 = 00001100:bit_width 4,highest Some(3),lowest Some(2),isolate_highest 8,isolate_lowest 4
- 40 = 00101000:bit_width 6,highest Some(5),lowest Some(3),isolate_highest 32,isolate_lowest 8
- 255 = 11111111:bit_width 8,highest Some(7),lowest Some(0),isolate_highest 128,isolate_lowest 1
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.97.1 | 2026-07-16 | 否 | rustc: Fix miscompilation in LLVM optimization This backports an LLVM submodule bump to include the LLVM-side fix and a revert of the rustc change that is one known trigger for the bug. The rustc side revert should not be strictly necessary but is done out of abundance of caution. |
已知漏洞
影響
1.97.0 的已知漏洞共 0 個;在本版修正的共 0 個;仍影響最新修補版 1.97.1 的共 0 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。