Rust 1.96

最後更新


Rust 1.96 發布於 2026-05-28,目前最新修補版為 1.96.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.96 新增 assert_matches! 與 debug_assert_matches!:檢查值是否符合某個模式,失敗時會印出值的 Debug 內容,比 assert!(matches!(..)) 好除錯得多。同一版穩定新的 core::range::Range 等範圍型別,它們實作 IntoIterator 而非 Iterator,因此可以是 Copy。升級時要注意:assert_matches! 沒有放進 prelude,使用前要從 std 或 core 匯入;WebAssembly 目標不再預設傳 --allow-undefined 給連結器,未定義的符號會變成連結錯誤。

語言變更


  • cfg 可以接收 expr 類型的巨集變數,詳見宣告式巨集。
  • tuple 運算式中的永不型別一律強制轉型。
  • 允許 ManuallyDrop 型別的常數當作模式(修正 1.94 引入的回歸)。
  • 支援 s390x 向量暫存器的內嵌組合語言。

標準庫重點


  • assert_matches!、debug_assert_matches! 斷言值符合模式,詳見單元測試。
  • core::range::Range、RangeFrom、RangeToInclusive 與對應的迭代器型別,詳見 std::ops。
  • NonZero 整數的範圍可以迭代,詳見數值型別。
  • AssertUnwindSafe<T>、LazyCell<T, F>、LazyLock<T, F> 實作 From<T>。

Cargo 與工具


  • 相依可以同時指定 git 倉庫與替代 registry:本機使用 git,發布時使用 registry 版本,詳見 Cargo。
  • 設定檔支援 target.'cfg(..)'.rustdocflags。
  • 修正 CVE-2026-5222 與 CVE-2026-5223。
  • rustdoc 側邊欄把方法與關聯函式分開列出,棄用說明改用一般文件的方式呈現。

相容性注意


  • WebAssembly 目標不再傳 --allow-undefined,需要舊行為時用 RUSTFLAGS=-Clink-arg=--allow-undefined 或 #[link(wasm_import_module = "env")]。
  • use S::{self as Other}; 這類對 struct 的 {self} 匯入不再允許,{self} 必須以模組為父路徑,詳見模組系統。
  • uninhabited_static lint 改為預設拒絕並在相依套件中回報。
  • 移除 -Csoft-float;外部 LLVM 最低版本提高到 21。
  • BTreeMap::append() 經過最佳化,Ord 實作不正確的型別可能因此 panic。

assert_matches!


assert_matches!(值, 模式) 檢查值是否符合模式,也可以加上 if 守衛。失敗時 panic 訊息會同時列出實際的值與期望的模式。範例以 panic::set_hook 自訂輸出,只印出 panic 訊息本身。
範例:
  1. use std::assert_matches;
  2. #[derive(Debug)]
  3. enum Status {
  4. Ready,
  5. Running(u32),
  6. }
  7. fn main() {
  8. assert_matches!(Status::Running(3), Status::Running(1..=5));
  9. assert_matches!(Status::Ready, Status::Ready);
  10. assert_matches!(Some(42), Some(n) if n % 2 == 0);
  11. println!("全部斷言通過");
  12. std::panic::set_hook(Box::new(|info| {
  13. println!("{}", info.payload_as_str().unwrap_or("?"));
  14. }));
  15. let r = std::panic::catch_unwind(|| {
  16. assert_matches!(Status::Running(9), Status::Running(1..=5));
  17. });
  18. println!("失敗:{}", r.is_err());
  19. }

輸出:
  1. 全部斷言通過
  2. assertion `left matches right` failed
  3. left: Running(9)
  4. right: Status::Running(1..=5)
  5. 失敗:true

注意:這兩個巨集不在 prelude 中,因為會與熱門第三方 crate 的同名巨集衝突,必須先 use std::assert_matches;(或 core::assert_matches)。

新的 Range 型別


0..10 產生的舊型別 std::ops::Range 本身就是迭代器,因此不能是 Copy,放進結構體後整個結構體也不能 Copy。新的 std::range::Range 只實作 IntoIterator,可以 Copy,欄位 start、end 公開,也能直接拿來索引字串與切片;要走訪時呼叫 into_iter()。目前 a..b 語法仍產生舊型別,用 Range::from 轉換。
範例:
  1. use std::range::Range;
  2. #[derive(Debug, Clone, Copy)]
  3. struct Span(Range<usize>);
  4. impl Span {
  5. fn of(self, s: &str) -> &str {
  6. &s[self.0]
  7. }
  8. }
  9. fn main() {
  10. let text = "hello world";
  11. let word = Span(Range::from(6..11));
  12. let copy = word;
  13. println!("{} / {}", word.of(text), copy.of(text));
  14. println!("start={} end={}", word.0.start, word.0.end);
  15. let total: usize = word.0.into_iter().sum();
  16. println!("6 到 10 加總 {total}");
  17. }

輸出:
  1. world / world
  2. start=6 end=11
  3. 6 到 10 加總 40

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.96.12026-06-30是Cargo: fix timeout/retry behavior;Cargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2;rustc: fix miscompilation in MIR optimization

已知漏洞


影響 1.96.0 的已知漏洞共 0 個;在本版修正的共 3 個;仍影響最新修補版 1.96.1 的共 0 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2025-15661cargoCargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2未公布1.96.1
CVE-2026-55199cargoCargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2未公布1.96.1
CVE-2026-55200cargoCargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2未公布1.96.1

資料來源