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_refslint,對取得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_fieldslint 併入dead_code。let-else中禁止使用大括號形式的巨集呼叫。
C 字串字面值
在字串前面加
c,編譯器會自動在結尾補上 \0,得到可以直接傳給 C 函式的 &CStr。字面值中間不能出現 \0,否則編譯失敗。範例:
- use std::ffi::CStr;
- fn main() {
- let greeting: &CStr = c"hello";
- println!("{:?}", greeting);
- println!("{:?}", greeting.to_bytes_with_nul());
- println!("{}", greeting.count_bytes());
- println!("{}", greeting.to_str().unwrap());
- }
輸出:
- "hello"
- [104, 101, 108, 108, 111, 0]
- 5
- hello
slice::chunk_by 與 first_chunk
chunk_by 依「相鄰兩個元素是否屬於同一組」把切片切成多段,例如找出連續遞增的區間;first_chunk::<N> 與 split_last_chunk::<N> 則一次取出固定長度的陣列參考,長度不足時回傳 None。範例:
- fn main() {
- let temps = [18, 19, 21, 20, 22, 25, 23];
- let runs: Vec<&[i32]> = temps.chunk_by(|a, b| a < b).collect();
- println!("{:?}", runs);
- let words = ["apple", "avocado", "banana", "blueberry", "cherry"];
- for group in words.chunk_by(|a, b| a.as_bytes()[0] == b.as_bytes()[0]) {
- println!("{:?}", group);
- }
- let data = [1, 2, 3, 4, 5];
- if let Some([a, b]) = data.first_chunk::<2>() {
- println!("前兩個:{a} {b}");
- }
- if let Some((head, rest)) = data.split_last_chunk::<3>() {
- println!("{:?} {:?}", head, rest);
- }
- }
輸出:
- [[18, 19, 21], [20, 22, 25], [23]]
- ["apple", "avocado"]
- ["banana", "blueberry"]
- ["cherry"]
- 前兩個:1 2
- [1, 2] [3, 4, 5]
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.77.1 | 2024-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.2 | 2024-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-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
仍影響 1.77.2
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| 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 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。