Rust 1.77

最後更新


Rust 1.77 發布於 2024-03-21,目前最新修補版為 1.77.2,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.77 帶來 C 字串字面值 c"..."、可遞迴的 async fn(需透過間接層,例如 Box::pin),以及計算欄位位移的 mem::offset_of!。對新手來說,和 C 函式庫互動時不必再手動補 \0;切片也多了 chunk_by、first_chunk 等好用的方法。升級時要注意:Cargo 的 release profile 在未要求除錯資訊時,預設會 strip 掉除錯資訊。

語言變更


  • 穩定 C 字串字面值 c"...",型別是 &CStr,詳見 FFI。
  • async fn 可以遞迴呼叫自己,只要透過間接層(如 Box::pin),詳見 非同步基礎。
  • 新增 static_mut_refs lint,對取得 static mut 的參考發出警告,詳見 unsafe。

標準庫重點


  • 新增 mem::offset_of!,取得結構欄位相對於開頭的位元組位移,詳見 std::mem。
  • 切片新增 chunk_by、first_chunk、last_chunk、split_first_chunk、split_last_chunk 及其 _mut 版本,詳見 切片。
  • 陣列新增 each_ref、each_mut,詳見 陣列。
  • 新增 File::create_new,檔案已存在時回傳錯誤而不是覆寫,詳見 std::fs。
  • 新增 Mutex::clear_poison、RwLock::clear_poison,詳見 std::sync。
  • 新增 f32::round_ties_even、f64::round_ties_even 與 Bound::map;core::net 穩定。

Cargo 與工具


  • 建置腳本指令可以寫成 cargo:: 前綴的新語法,詳見 Cargo 與套件。
  • 未要求除錯資訊的 profile(如 debug = 0)預設啟用 strip = "debuginfo"。
  • 棄用 Cargo 中的 rustc 外掛支援。

相容性注意


  • 在 x86 系列目標上,i128/u128 改為 16 位元組對齊。
  • unused_tuple_struct_fields lint 併入 dead_code。
  • let-else 中禁止使用大括號形式的巨集呼叫。

C 字串字面值


在字串前面加 c,編譯器會自動在結尾補上 \0,得到可以直接傳給 C 函式的 &CStr。字面值中間不能出現 \0,否則編譯失敗。
範例:
  1. use std::ffi::CStr;
  2. fn main() {
  3. let greeting: &CStr = c"hello";
  4. println!("{:?}", greeting);
  5. println!("{:?}", greeting.to_bytes_with_nul());
  6. println!("{}", greeting.count_bytes());
  7. println!("{}", greeting.to_str().unwrap());
  8. }

輸出:
  1. "hello"
  2. [104, 101, 108, 108, 111, 0]
  3. 5
  4. hello

slice::chunk_by 與 first_chunk


chunk_by 依「相鄰兩個元素是否屬於同一組」把切片切成多段,例如找出連續遞增的區間;first_chunk::<N> 與 split_last_chunk::<N> 則一次取出固定長度的陣列參考,長度不足時回傳 None。
範例:
  1. fn main() {
  2. let temps = [18, 19, 21, 20, 22, 25, 23];
  3. let runs: Vec<&[i32]> = temps.chunk_by(|a, b| a < b).collect();
  4. println!("{:?}", runs);
  5. let words = ["apple", "avocado", "banana", "blueberry", "cherry"];
  6. for group in words.chunk_by(|a, b| a.as_bytes()[0] == b.as_bytes()[0]) {
  7. println!("{:?}", group);
  8. }
  9. let data = [1, 2, 3, 4, 5];
  10. if let Some([a, b]) = data.first_chunk::<2>() {
  11. println!("前兩個:{a} {b}");
  12. }
  13. if let Some((head, rest)) = data.split_last_chunk::<3>() {
  14. println!("{:?} {:?}", head, rest);
  15. }
  16. }

輸出:
  1. [[18, 19, 21], [20, 22, 25], [23]]
  2. ["apple", "avocado"]
  3. ["banana", "blueberry"]
  4. ["cherry"]
  5. 前兩個:1 2
  6. [1, 2] [3, 4, 5]

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.77.12024-03-28否Revert stripping debuginfo by default for Windows This fixes a regression in 1.77 by reverting to the previous default. Platforms other than Windows are not affected.;Internal: Fix heading anchor rendering in doc pages
1.77.22024-04-09是CVE-2024-24576: fix escaping of Windows batch file arguments in std::process::Command

已知漏洞


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

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2024-24576stdstd::process::Command did not properly escape arguments of batch files on Windows早於 1.77.21.77.2

仍影響 1.77.2


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

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

資料來源