Rust 1.93

最後更新


Rust 1.93 發布於 2026-01-22,目前最新修補版為 1.93.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.93 把 *-linux-musl 目標內附的 musl 更新到 1.2.5,靜態連結的 Linux 執行檔在網路與 DNS 解析上更可靠。標準庫新增 fmt::from_fn,用一個閉包就能做出可以 {} 格式化的值,不必為此定義新型別。升級時要注意:musl 1.2.4 移除了一些舊的相容符號,很舊的 libc crate(0.2.146 以前)需要更新;#[test] 放在沒有意義的位置(例如 struct 上)會變成錯誤。

語言變更


  • system ABI 可宣告 C 風格可變參數函式。
  • 穩定 asm_cfg:asm! 區塊中的個別語句可以加 cfg 屬性,詳見 unsafe。
  • 新增預設警告的 const_item_interior_mutations lint,提醒呼叫會修改具內部可變性 const 項目的方法;以及 function_casts_as_integer lint。
  • 某些關鍵字用作 cfg 條件時會報錯。

標準庫重點


  • std::fmt::from_fn 與 std::fmt::FromFn:用閉包實作格式化,詳見 std::fmt。
  • VecDeque::pop_front_if、pop_back_if 只在符合條件時取出,詳見集合。
  • <[T]>::as_array 把切片轉成固定長度陣列的參考,長度不符回傳 None,詳見陣列。
  • String::into_raw_parts、Vec::into_raw_parts、Duration::from_nanos_u128 與 [MaybeUninit<T>] 的多個方法。
  • 全域配置器可以使用 thread_local! 與 std::thread::current()。
  • BTreeMap::append 遇到已存在的鍵時不再更新該鍵。

Cargo 與工具


  • cargo clean 新增 --workspace;cargo tree --format 支援長格式變數名稱。
  • build script 會依 profile 設定 CARGO_CFG_DEBUG_ASSERTIONS。
  • rustdoc 移除 #![doc(document_private_items)],並驗證 crate 層級 doc 屬性的值。

相容性注意


  • 內附 musl 更新到 1.2.5,移除了舊版 libc crate 使用的相容符號。
  • #[test] 放在 trait 方法、型別等無意義位置會報錯,詳見單元測試。
  • deref_nullptr lint 改為預設拒絕。
  • 依賴 static-init 1.0.1 到 1.0.3 的 crate 會因 CARGO_CFG_DEBUG_ASSERTIONS 編譯失敗。
  • 標準庫不再在 Copy 上使用 specialization,部分 API 可能改呼叫 Clone::clone,效能可能略降。

fmt::from_fn


想讓某個值能用 {} 印出,通常要定義一個型別並實作 Display。fmt::from_fn 接收一個 |f| -> fmt::Result 閉包,回傳一個實作 Display 的值,適合「只是想客製一次輸出格式」的情況;它也能直接呼叫 to_string()。
範例:
  1. use std::fmt;
  2. fn list<'a>(items: &'a [&'a str]) -> impl fmt::Display + 'a {
  3. fmt::from_fn(move |f| {
  4. for (i, item) in items.iter().enumerate() {
  5. if i > 0 {
  6. write!(f, "、")?;
  7. }
  8. write!(f, "「{item}」")?;
  9. }
  10. Ok(())
  11. })
  12. }
  13. fn main() {
  14. let fruits = ["蘋果", "香蕉", "芭樂"];
  15. println!("水果:{}", list(&fruits));
  16. let text = list(&fruits).to_string();
  17. println!("{} 個字元", text.chars().count());
  18. }

輸出:
  1. 水果:「蘋果」、「香蕉」、「芭樂」
  2. 14 個字元

VecDeque::pop_front_if


VecDeque 是兩端都能快速進出的佇列。pop_front_if 先檢查前端元素是否符合條件,符合才取出並回傳 Some,否則保持原樣回傳 None;搭配 while let 可以「一直處理到遇到第一個不符合的」。pop_back_if 則作用在尾端。
範例:
  1. use std::collections::VecDeque;
  2. fn main() {
  3. let mut tasks: VecDeque<u32> = VecDeque::from([1, 2, 7, 3, 9]);
  4. while let Some(t) = tasks.pop_front_if(|t| *t < 5) {
  5. println!("處理 {t}");
  6. }
  7. println!("停在 {:?}", tasks.front());
  8. let last = tasks.pop_back_if(|t| *t % 2 == 0);
  9. println!("尾端是偶數才取出:{last:?}");
  10. println!("{tasks:?}");
  11. }

輸出:
  1. 處理 1
  2. 處理 2
  3. 停在 Some(7)
  4. 尾端是偶數才取出:None
  5. [7, 3, 9]

切片的 as_array


函式常收到 &[u8],但後續邏輯需要固定長度。as_array::<N>() 在長度剛好是 N 時回傳 Some(&[T; N]),否則回傳 None,搭配 ? 就能乾淨地處理長度錯誤。
範例:
  1. fn rgb(bytes: &[u8]) -> Option<(u8, u8, u8)> {
  2. let [r, g, b] = bytes.as_array::<3>()?;
  3. Some((*r, *g, *b))
  4. }
  5. fn main() {
  6. println!("{:?}", rgb(&[255, 128, 0]));
  7. println!("{:?}", rgb(&[1, 2]));
  8. println!("{:?}", rgb(&[1, 2, 3, 4]));
  9. }

輸出:
  1. Some((255, 128, 0))
  2. None
  3. None

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.93.12026-02-12否Don't try to recover keyword as non-keyword identifier, fixing an ICE that especially affected rustfmt.;Fix clippy::panicking_unwrap false-positive on field access with implicit deref.;Revert "Update wasm-related dependencies in CI", fixing file descriptor leaks on the wasm32-wasip2 target.

已知漏洞


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

仍影響 1.93.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。

資料來源