Rust 1.94

最後更新


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

版本介紹


Rust 1.94 為切片新增 array_windows:和 windows 一樣產生滑動視窗,但每個視窗是固定長度的陣列 &[T; N],長度甚至能從閉包的解構模式推斷出來。Cargo 開始支援在設定檔中用 include 載入其他設定檔,也改用 TOML 1.1 解析 Cargo.toml。升級時要注意:在 Cargo.toml 使用 TOML 1.1 新語法會提高開發時的最低 Rust 版本;標準庫巨集改由 prelude 匯入,自訂同名巨集再以 glob 匯入時可能出現名稱衝突。

語言變更


  • impl 與 impl 項目繼承對應 trait 與 trait 項目的 dead_code lint 等級,詳見 trait。
  • 新增預設警告的 unused_visibilities lint,針對 const _ 宣告上的可見性修飾。
  • Unicode 更新到 17 版。
  • 修正閉包中某些不正確的生命週期錯誤,詳見閉包。

標準庫重點


  • <[T]>::array_windows 產生固定長度的滑動視窗,詳見切片。
  • <[T]>::element_offset 取得元素在切片中的索引。
  • LazyCell、LazyLock 新增 get、get_mut、force_mut,可以查詢是否已初始化,詳見 std::sync 與 std::cell。
  • Peekable::next_if_map、next_if_map_mut,詳見迭代器。
  • 新增 f32/f64 的 consts::EULER_GAMMA、consts::GOLDEN_RATIO,以及 impl TryFrom<char> for usize,詳見 f64。
  • BinaryHeap<T> 部分方法放寬 T: Ord 限制;f32::mul_add、f64::mul_add 可在 const 情境使用。

Cargo 與工具


  • 穩定設定檔的 include 鍵,.cargo/config.toml 可以載入其他設定檔,並可標記為 optional,詳見 Cargo。
  • Cargo 改用 TOML v1.1 解析 manifest 與設定檔,例如 inline table 可以跨行並加結尾逗號。
  • registry index 穩定 pubtime 欄位,記錄版本的發布時間。
  • CARGO_BIN_EXE_<crate> 在執行期也能取得,詳見整合測試。

相容性注意


  • 標準庫巨集改由 prelude 匯入:自訂的同名巨集(例如 matches)以 glob 匯入時會產生歧義,需要明確匯入,詳見宣告式巨集。
  • 閉包捕獲與模式比對的互動改得一致且正確,部分非 move 閉包可能改成只移動變數的一部分,造成新的借用錯誤或 Drop 時機改變。
  • 運算式位置的 include! 不再去除 shebang 行。
  • 跨 crate 的歧義 glob 重新匯出也會報錯。
  • 生命週期識別字(如 'a)會做 NFC 正規化。
  • Windows 上 SystemTime::checked_sub_duration 對 1601-01-01 之前的時間回傳 None。

array_windows


windows(2) 產生的是長度不固定的切片 &[T],要用 w[0]、w[1] 取值。array_windows() 產生 &[T; N],可以直接在閉包參數寫 [a, b] 解構,編譯器從模式推斷出 N = 2;範例第二段寫 [a, b, c],N 就是 3。
範例:
  1. fn main() {
  2. let temps = [18, 21, 25, 24, 30];
  3. for [a, b] in temps.array_windows() {
  4. println!("{a} -> {b}:{:+}", b - a);
  5. }
  6. let rising = temps
  7. .array_windows()
  8. .filter(|[a, b, c]| a < b && b < c)
  9. .count();
  10. println!("連續三天上升的區段:{rising}");
  11. }

輸出:
  1. 18 -> 21:+3
  2. 21 -> 25:+4
  3. 25 -> 24:-1
  4. 24 -> 30:+6
  5. 連續三天上升的區段:1

Peekable::next_if_map


Peekable 是可以「偷看」下一個元素的迭代器。next_if_map 把下一個元素交給閉包:閉包回傳 Ok(值) 就消耗該元素並回傳 Some(值);回傳 Err(原元素) 就把元素放回去、回傳 None。適合「一邊解析一邊轉換,遇到不符合的就停下」。
範例:
  1. fn main() {
  2. let tokens = ["12", "7", "x", "3"];
  3. let mut iter = tokens.iter().peekable();
  4. let mut numbers = Vec::new();
  5. while let Some(n) = iter.next_if_map(|s| s.parse::<i32>().map_err(|_| s)) {
  6. numbers.push(n);
  7. }
  8. println!("數字:{numbers:?}");
  9. println!("停在:{:?}", iter.next());
  10. }

輸出:
  1. 數字:[12, 7]
  2. 停在:Some("x")

LazyLock::get


LazyLock 讓全域變數在第一次使用時才初始化。新的 LazyLock::get 在不觸發初始化的情況下查詢目前狀態:還沒初始化回傳 None,已初始化回傳 Some(&值)。範例中「初始化設定」只印一次,且出現在第一次讀取 CONFIG.len() 時。
範例:
  1. use std::sync::LazyLock;
  2. static CONFIG: LazyLock<Vec<&str>> = LazyLock::new(|| {
  3. println!("初始化設定");
  4. vec!["debug", "color"]
  5. });
  6. fn main() {
  7. println!("初始化前:{:?}", LazyLock::get(&CONFIG));
  8. println!("長度:{}", CONFIG.len());
  9. println!("初始化後:{:?}", LazyLock::get(&CONFIG));
  10. }

輸出:
  1. 初始化前:None
  2. 初始化設定
  3. 長度:2
  4. 初始化後:Some(["debug", "color"])

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.94.12026-03-26是Fix std::thread::spawn on wasm32-wasip1-threads;Remove new methods added to std::os::windows::fs::OpenOptionsExt The new methods were unstable, but the trait itself is not sealed and so cannot be extended with non-default methods.;Clippy: fix ICE in match_same_arms;Cargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details.

已知漏洞


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

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2026-33055cargoCargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details.未公布1.94.1
CVE-2026-33056cargoCargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details.未公布1.94.1

仍影響 1.94.1


編號元件摘要(官方原文)影響範圍修正於
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

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

資料來源