Rust 1.88

最後更新


Rust 1.88 發布於 2025-06-26,目前最新修補版為 1.88.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.88 最受矚目的是 let chains:在 if 與 while 的條件裡,可以用 && 把多個 let 模式比對和一般布林條件串在一起,原本要層層巢狀的 if let 終於能攤平成一行一行。對新手來說,這讓「先確認有值、再檢查值的內容」這種常見邏輯好寫又好讀。升級時要注意:let chains 只在 2024 edition 可用,舊 edition 的 crate 需先升級 edition(見 Edition 演進);Cargo 也開始自動清理長期沒用到的下載快取。

語言變更


  • 2024 edition 穩定 let chains,if/while 條件可混用 let 與布林運算,詳見 if 條件判斷與迴圈。
  • 穩定裸函式(naked functions):以 #[unsafe(naked)] 標記、函式本體只有一個 naked_asm!,編譯器不產生序言與結尾,屬於 unsafe 的低階用途。
  • cfg 條件可以直接寫布林字面值 #[cfg(true)]、#[cfg(false)],取代語意不直觀的 cfg(all())、cfg(any())。
  • 新增預設警告的 dangerous_implicit_autorefs lint 與 invalid_null_arguments lint,針對原始指標的誤用。

標準庫重點


  • Cell::update 用閉包讀取並更新 Cell 內的值。
  • HashMap::extract_if、HashSet::extract_if 邊走訪邊移除符合條件的元素,詳見 HashMap。
  • 切片新增 as_chunks、as_rchunks 等方法,把切片切成固定長度的陣列區塊。
  • 新增 hint::select_unpredictable、mod ffi::c_str,以及 *const T/*mut T 的 Default 實作。
  • 保證 [T; N]::from_fn 依索引由小到大產生元素,{float}::NAN 保證是 quiet NaN。

Cargo 與工具


  • Cargo 穩定自動垃圾回收:網路下載超過 3 個月沒用到、本機取得超過 1 個月沒用到的快取檔案會自動刪除;--offline/--frozen 時不執行,詳見 Cargo。
  • 測試框架的 --nocapture 旗標改名為 --no-capture,舊名標記為棄用。
  • rustdoc 可以用 ignore-* 屬性依目標平台略過文件測試。

相容性注意


  • #[bench] 屬性完全取消穩定,沒有開啟 custom_test_frameworks 時直接是編譯錯誤。
  • 修正借用檢查對某些「恆為真」模式過於寬鬆的問題,原本能編譯的錯誤程式現在會被擋下。
  • 部分過去被意外接受的宣告式巨集寫法現在會被拒絕,多半改用 tt 片段即可修正。

let chains


以前要同時「解開 Option」又「檢查內容」,得寫兩三層巢狀的 if let 與 if。現在用 && 串起來,前面 let 綁定的變數(如 u、age)可以在後面的條件與本體中使用;任一段不成立就走 else。while let 也能這樣串,範例中取出 8 時條件不成立,迴圈結束,8 已被 pop 取出。
範例:
  1. struct User {
  2. name: String,
  3. age: Option<u32>,
  4. }
  5. fn describe(user: Option<&User>) -> String {
  6. if let Some(u) = user
  7. && let Some(age) = u.age
  8. && age >= 18
  9. {
  10. format!("{} 已成年({} 歲)", u.name, age)
  11. } else {
  12. String::from("資料不足或未成年")
  13. }
  14. }
  15. fn main() {
  16. let a = User { name: String::from("小明"), age: Some(20) };
  17. let b = User { name: String::from("小華"), age: Some(15) };
  18. let c = User { name: String::from("阿美"), age: None };
  19. println!("{}", describe(Some(&a)));
  20. println!("{}", describe(Some(&b)));
  21. println!("{}", describe(Some(&c)));
  22. println!("{}", describe(None));
  23. let mut stack = vec![3, 8, 1];
  24. while let Some(top) = stack.pop()
  25. && top != 8
  26. {
  27. println!("取出 {top}");
  28. }
  29. println!("剩下 {stack:?}");
  30. }

輸出:
  1. 小明 已成年(20 歲)
  2. 資料不足或未成年
  3. 資料不足或未成年
  4. 資料不足或未成年
  5. 取出 1
  6. 剩下 [3]

注意:這個語法只在 2024 edition 可用;在 2021 以前的 edition 會得到編譯錯誤。

Cell::update


Cell 讓你透過共用參考 &self 修改內部的值(內部可變性)。以前要寫 c.set(c.get() + 1),現在 update 接收一個閉包,把舊值換成閉包算出的新值,一步完成。
範例:
  1. use std::cell::Cell;
  2. struct Counter {
  3. hits: Cell<u32>,
  4. }
  5. impl Counter {
  6. fn record(&self) {
  7. self.hits.update(|n| n + 1);
  8. }
  9. }
  10. fn main() {
  11. let c = Counter { hits: Cell::new(0) };
  12. for _ in 0..3 {
  13. c.record();
  14. }
  15. println!("hits = {}", c.hits.get());
  16. let level = Cell::new(10);
  17. level.update(|n| n * 2);
  18. println!("level = {}", level.get());
  19. }

輸出:
  1. hits = 3
  2. level = 20

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.88.0


編號元件摘要(官方原文)影響範圍修正於
CVE-2026-5222cargoCargo can be coerced to share credentials between registries早於 1.96.01.96.0
CVE-2026-5223cargoCrates in third party registries can override the cached source of other crates早於 1.96.01.96.0
CVE-2025-11233stdRust standard library didn't detect all path separators on Cygwin1.87.0 起,早於 1.89.01.89.0

本版已停止支援,不會再修正這些漏洞;請升級到 1.100.1。

資料來源