Rust 1.71

最後更新


Rust 1.71 發布於 2023-07-13,目前最新修補版為 1.71.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.71 主要強化與其他語言的互通:extern "C-unwind" 等 -unwind ABI 穩定,讓 panic 或 C++ 例外跨越語言邊界時有明確定義的行為;Windows 上的 raw-dylib 連結方式讓綁定 DLL 不必在建置時提供函式庫檔。標準庫則能在陣列與元組之間互相轉換,並補上 BuildHasher::hash_one。升級要注意:*-linux-musl 目標改用 musl 1.2.3;在 panic hook 裡再次 panic 會直接中止行程。

語言變更


  • raw-dylib、link_ordinal、import_name_type 與 -Cdlltool 穩定,FFI 見 FFI。
  • drop_ref、forget_ref、drop_copy、forget_copy 這組 lint 從 Clippy 移入 rustc,提醒對參考或 Copy 值呼叫 drop/forget 沒有效果。
  • 型別推論在受約束的變數周圍更保守。
  • extern "C-unwind" 與其他 -unwind ABI 穩定;既有的 extern "C" 在未來版本可能改變跨語言 unwind 的行為。
  • #[debugger_visualizer] 穩定,函式庫可以內嵌 Natvis 或 GDB 腳本,改善除錯器顯示。

標準庫重點


  • 長度 1 到 12 的陣列與同質元組可以互相轉換:From<[T; N]> for (T, ...) 與 From<(T, ...)> for [T; N],詳見 元組與陣列。
  • BuildHasher::hash_one 一次算出某個值的雜湊,詳見 std::hash。
  • thread_local! 的 const { ... } 初始化寫法(1.59 起已可用)補進文件,詳見 std::thread。
  • NonZeroI* 新增 is_positive、is_negative、checked_neg 等方法並實作 Neg;CStr::is_empty 穩定。
  • ptr::read、<[T]>::split_at 等可在 const 環境使用。

Cargo 與工具


  • Cargo.toml 的 profile 可以用名稱設定除錯資訊等級;cargo new/cargo init 會自動繼承工作區欄位,見 工作區。
  • rustdoc 新增 rustdoc::unescaped_backticks lint。

相容性注意


  • *-linux-musl 目標升級到 musl 1.2.3,32 位元系統啟用 time64。
  • 遞迴 panic 處理改寫:unwind 過程中的額外 panic 只要在離開 Drop 前被捕捉就允許,但 panic hook 中的 panic 會立即中止,見 panic。
  • TypeId 不再能用在模式比對中;使用 log 的 kv_unstable feature 需升級到 log v0.4.18 以上。
  • Cargo 的 [env] 設定表不得設定 RUSTUP_HOME、RUSTUP_TOOLCHAIN。

陣列與元組互相轉換


同型別元素的元組(例如 (i32, i32, i32))與陣列 [i32; 3] 現在可以用 From/Into 互轉。元組適合解構與當函式回傳值,陣列適合迭代與索引,需要哪一種就轉成哪一種,轉換特徵見 std::convert。
範例:
  1. fn main() {
  2. let point: (i32, i32, i32) = [3, 4, 5].into();
  3. println!("{point:?}");
  4. let rgb: [u8; 3] = (255, 128, 0).into();
  5. println!("{rgb:?} {}", rgb.iter().map(|c| format!("{c:02x}")).collect::<String>());
  6. }

輸出:
  1. (3, 4, 5)
  2. [255, 128, 0] ff8000

注意:只支援長度 1 到 12,而且元組的每個元素必須是同一個型別。

const 初始化的 thread_local


thread_local! 宣告「每條執行緒各有一份」的變數;初始值寫成 const { ... } 時,編譯器不必在每次存取時檢查是否已初始化,產生的程式碼更有效率。下面主執行緒加了兩次、另一條執行緒只加一次,兩者互不影響。
範例:
  1. use std::cell::Cell;
  2. use std::thread;
  3. thread_local! {
  4. static COUNTER: Cell<u32> = const { Cell::new(0) };
  5. }
  6. fn bump() -> u32 {
  7. COUNTER.with(|c| {
  8. c.set(c.get() + 1);
  9. c.get()
  10. })
  11. }
  12. fn main() {
  13. bump();
  14. bump();
  15. let other = thread::spawn(|| bump()).join().unwrap();
  16. println!("main={} other={other}", COUNTER.with(|c| c.get()));
  17. }

輸出:
  1. main=2 other=1

BuildHasher::hash_one


BuildHasher::hash_one 省去「建立 hasher、餵資料、finish」三步。&str 與 String 的雜湊結果相同,這正是 HashMap<String, _> 能用 &str 查詢的原因。雜湊值本身每次執行都不同,所以範例只印比較結果。
範例:
  1. use std::collections::hash_map::RandomState;
  2. use std::hash::BuildHasher;
  3. fn main() {
  4. let state = RandomState::new();
  5. let a = state.hash_one("rust");
  6. let b = state.hash_one(String::from("rust"));
  7. let c = state.hash_one("go");
  8. println!("{} {}", a == b, a == c);
  9. }

輸出:
  1. true false

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.71.12023-08-03是Fix CVE-2023-38497: Cargo did not respect the umask when extracting dependencies;Fix bash completion for users of Rustup;Do not show suspicious_double_ref_op lint when calling borrow();Fix ICE: substitute types before checking inlining compatibility;Fix ICE: don't use can_eq in derive(..) suggestion for missing method;Fix building Rust 1.71.0 from the source tarball

已知漏洞


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

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2023-38497cargoCargo not respecting umask when extracting crate archives早於 1.71.11.71.1

仍影響 1.71.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
CVE-2024-43402stdstd::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods早於 1.81.01.81.0
CVE-2024-24576stdstd::process::Command did not properly escape arguments of batch files on Windows早於 1.77.21.77.2
CVE-2023-40030cargoMalicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports1.60.0 起,早於 1.72.01.72.0

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

資料來源