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 上)會變成錯誤。語言變更
systemABI 可宣告 C 風格可變參數函式。- 穩定
asm_cfg:asm!區塊中的個別語句可以加cfg屬性,詳見 unsafe。 - 新增預設警告的
const_item_interior_mutationslint,提醒呼叫會修改具內部可變性const項目的方法;以及function_casts_as_integerlint。 - 某些關鍵字用作
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,移除了舊版
libccrate 使用的相容符號。 #[test]放在 trait 方法、型別等無意義位置會報錯,詳見單元測試。deref_nullptrlint 改為預設拒絕。- 依賴
static-init1.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()。範例:
- use std::fmt;
- fn list<'a>(items: &'a [&'a str]) -> impl fmt::Display + 'a {
- fmt::from_fn(move |f| {
- for (i, item) in items.iter().enumerate() {
- if i > 0 {
- write!(f, "、")?;
- }
- write!(f, "「{item}」")?;
- }
- Ok(())
- })
- }
- fn main() {
- let fruits = ["蘋果", "香蕉", "芭樂"];
- println!("水果:{}", list(&fruits));
- let text = list(&fruits).to_string();
- println!("{} 個字元", text.chars().count());
- }
輸出:
- 水果:「蘋果」、「香蕉」、「芭樂」
- 14 個字元
VecDeque::pop_front_if
VecDeque 是兩端都能快速進出的佇列。pop_front_if 先檢查前端元素是否符合條件,符合才取出並回傳 Some,否則保持原樣回傳 None;搭配 while let 可以「一直處理到遇到第一個不符合的」。pop_back_if 則作用在尾端。範例:
- use std::collections::VecDeque;
- fn main() {
- let mut tasks: VecDeque<u32> = VecDeque::from([1, 2, 7, 3, 9]);
- while let Some(t) = tasks.pop_front_if(|t| *t < 5) {
- println!("處理 {t}");
- }
- println!("停在 {:?}", tasks.front());
- let last = tasks.pop_back_if(|t| *t % 2 == 0);
- println!("尾端是偶數才取出:{last:?}");
- println!("{tasks:?}");
- }
輸出:
- 處理 1
- 處理 2
- 停在 Some(7)
- 尾端是偶數才取出:None
- [7, 3, 9]
切片的 as_array
函式常收到
&[u8],但後續邏輯需要固定長度。as_array::<N>() 在長度剛好是 N 時回傳 Some(&[T; N]),否則回傳 None,搭配 ? 就能乾淨地處理長度錯誤。範例:
- fn rgb(bytes: &[u8]) -> Option<(u8, u8, u8)> {
- let [r, g, b] = bytes.as_array::<3>()?;
- Some((*r, *g, *b))
- }
- fn main() {
- println!("{:?}", rgb(&[255, 128, 0]));
- println!("{:?}", rgb(&[1, 2]));
- println!("{:?}", rgb(&[1, 2, 3, 4]));
- }
輸出:
- Some((255, 128, 0))
- None
- None
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.93.1 | 2026-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-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 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。