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_autorefslint 與invalid_null_argumentslint,針對原始指標的誤用。
標準庫重點
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 取出。範例:
- struct User {
- name: String,
- age: Option<u32>,
- }
- fn describe(user: Option<&User>) -> String {
- if let Some(u) = user
- && let Some(age) = u.age
- && age >= 18
- {
- format!("{} 已成年({} 歲)", u.name, age)
- } else {
- String::from("資料不足或未成年")
- }
- }
- fn main() {
- let a = User { name: String::from("小明"), age: Some(20) };
- let b = User { name: String::from("小華"), age: Some(15) };
- let c = User { name: String::from("阿美"), age: None };
- println!("{}", describe(Some(&a)));
- println!("{}", describe(Some(&b)));
- println!("{}", describe(Some(&c)));
- println!("{}", describe(None));
- let mut stack = vec![3, 8, 1];
- while let Some(top) = stack.pop()
- && top != 8
- {
- println!("取出 {top}");
- }
- println!("剩下 {stack:?}");
- }
輸出:
- 小明 已成年(20 歲)
- 資料不足或未成年
- 資料不足或未成年
- 資料不足或未成年
- 取出 1
- 剩下 [3]
注意:這個語法只在 2024 edition 可用;在 2021 以前的 edition 會得到編譯錯誤。
Cell::update
Cell 讓你透過共用參考
&self 修改內部的值(內部可變性)。以前要寫 c.set(c.get() + 1),現在 update 接收一個閉包,把舊值換成閉包算出的新值,一步完成。範例:
- use std::cell::Cell;
- struct Counter {
- hits: Cell<u32>,
- }
- impl Counter {
- fn record(&self) {
- self.hits.update(|n| n + 1);
- }
- }
- fn main() {
- let c = Counter { hits: Cell::new(0) };
- for _ in 0..3 {
- c.record();
- }
- println!("hits = {}", c.hits.get());
- let level = Cell::new(10);
- level.update(|n| n * 2);
- println!("level = {}", level.get());
- }
輸出:
- hits = 3
- level = 20
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.88.0 的已知漏洞共 3 個;在本版修正的共 0 個;仍影響最新修補版 1.88.0 的共 3 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.88.0
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| CVE-2026-5222 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2025-11233 | std | Rust standard library didn't detect all path separators on Cygwin | 1.87.0 起,早於 1.89.0 | 1.89.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。