Rust 1.91

最後更新


Rust 1.91 發布於 2025-10-30,目前最新修補版為 1.91.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.91 把 aarch64-pc-windows-msvc(ARM64 Windows)升為 Tier 1,享有最高等級的測試保證。語言方面新增預設警告的 dangling_pointers_from_locals lint:函式回傳指向區域變數的原始指標時會被提醒。標準庫一口氣穩定大量 API,包括 strict_* 整數運算、Path::file_prefix、Duration::from_mins 等。升級時要注意:panic 訊息開始印出執行緒 ID,semicolon_in_expressions_from_macros 改為預設拒絕,屬性解析也變得更嚴格。

語言變更


  • 新增 dangling_pointers_from_locals lint,警告回傳指向區域變數的原始指標;回傳本身不是 unsafe,解參考才是。
  • 新增預設警告的 integer_to_ptr_transmutes lint,針對把整數 transmute 成指標。
  • 模式綁定依書寫順序處理,drop 順序以主要綁定的順序為準。
  • sysv64、win64、efiapi、aapcs ABI 可宣告 C 風格可變參數函式。
  • 新增 target_env = "macabi"、target_env = "sim" 兩個 cfg。

標準庫重點


  • 整數新增 strict_add、strict_sub、strict_mul、strict_pow 等 strict_* 方法,溢位時一律 panic,詳見數值型別。
  • Path::file_prefix、PathBuf::add_extension、PathBuf::with_added_extension,以及 Path/PathBuf 與 str/String 的 == 比較,詳見 std::path。
  • Duration::from_mins、Duration::from_hours,詳見 std::time。
  • str::floor_char_boundary、str::ceil_char_boundary 找最近的字元邊界,詳見 str。
  • PanicHookInfo::payload_as_str 直接取得 panic 訊息字串,詳見 std::panic。
  • BTreeMap::extract_if、BTreeSet::extract_if、core::iter::chain、core::array::repeat、Ipv4Addr::from_octets 與 AtomicPtr 的 fetch_* 系列。
  • panic 訊息會印出執行緒 ID;設定執行緒堆疊大小失敗時改回傳錯誤,不再 panic,詳見 std::thread。

Cargo 與工具


  • 穩定 build.build-dir 設定,指定中間建置產物的存放目錄,詳見 Cargo。
  • --target 與 build.target 可以寫 "host-tuple",自動代換成本機的目標三元組。
  • rustdoc 搜尋支援以原始指標型別查詢,例如 *const u8 ->。

相容性注意


  • semicolon_in_expressions_from_macros 改為預設拒絕:表達式位置的 macro_rules! 巨集展開結果不能以分號結尾,詳見宣告式巨集。
  • 屬性解析更嚴格,格式錯誤的 #[should_panic]、#[link] 等會直接報錯。
  • 名稱解析中的棄用 lint 全部改為預設拒絕,並在相依套件中回報。
  • 2024 edition 中,if let 條件裡由 pin!、format_args!、write!、writeln! 產生的暫存值作用範圍縮短。
  • Apple 平台連結時一律傳入 SDK 根目錄,/usr/local/lib 的函式庫可能不再自動連結。
  • 以 static 標記的閉包在語法上不再合法。

strict 整數運算


Rust 的整數溢位在 debug 建置會 panic、release 建置預設會繞回,行為隨建置模式不同。strict_* 系列不論建置模式,溢位時一律 panic,適合「溢位就是程式錯誤」的場合。範例用 panic::set_hook 自訂 panic 訊息,並以 catch_unwind 接住 panic;payload_as_str 也是這版穩定的 API。
範例:
  1. use std::panic;
  2. fn main() {
  3. println!("{}", 200u8.strict_add(50));
  4. println!("{}", (-7i32).strict_abs());
  5. println!("{}", 3u32.strict_pow(4));
  6. panic::set_hook(Box::new(|info| {
  7. println!("panic: {}", info.payload_as_str().unwrap_or("(非字串)"));
  8. }));
  9. let result = panic::catch_unwind(|| 200u8.strict_add(100));
  10. println!("結果是 Err:{}", result.is_err());
  11. }

輸出:
  1. 250
  2. 7
  3. 81
  4. panic: attempt to add with overflow
  5. 結果是 Err:true

注意:若要在溢位時改走其他流程而不是 panic,改用 checked_* 系列,它回傳 Option。

Path::file_prefix 與 add_extension


file_stem 只拿掉最後一個副檔名,report.tar.gz 得到 report.tar;file_prefix 則取第一個 . 之前的部分,得到 report。add_extension 在既有副檔名後面再加一個,而不是取代;with_added_extension 是回傳新 PathBuf 的版本。
範例:
  1. use std::path::{Path, PathBuf};
  2. fn main() {
  3. let p = Path::new("backup/report.tar.gz");
  4. println!("file_stem = {:?}", p.file_stem());
  5. println!("file_prefix = {:?}", p.file_prefix());
  6. let mut log = PathBuf::from("logs/app.log");
  7. log.add_extension("1");
  8. println!("{}", log.display());
  9. let zipped = log.with_added_extension("gz");
  10. println!("{}", zipped.display());
  11. println!("{}", Path::new("a/b") == "a/b");
  12. }

輸出:
  1. file_stem = Some("report.tar")
  2. file_prefix = Some("report")
  3. logs/app.log.1
  4. logs/app.log.1.gz
  5. true

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.91.12025-11-10否Enable file locking support in illumos. This fixes Cargo not locking the build directory on illumos.;Fix wasm_import_module attribute cross-crate. This fixes linker errors on WASM targets.

已知漏洞


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

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

資料來源