# 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](/lowlevel-ffi)。
- `async fn` 可以遞迴呼叫自己，只要透過間接層（如 `Box::pin`），詳見 [非同步基礎](/async-basics)。
- 新增 `static_mut_refs` lint，對取得 `static mut` 的參考發出警告，詳見 [unsafe](/lowlevel-unsafe)。

### 標準庫重點

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

### Cargo 與工具

- 建置腳本指令可以寫成 `cargo::` 前綴的新語法，詳見 [Cargo 與套件](/modules-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`，否則編譯失敗。

範例：

```rust
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());
}
```

輸出：

```text
"hello"
[104, 101, 108, 108, 111, 0]
5
hello
```

### slice::chunk_by 與 first_chunk

`chunk_by` 依「相鄰兩個元素是否屬於同一組」把切片切成多段，例如找出連續遞增的區間；`first_chunk::<N>` 與 `split_last_chunk::<N>` 則一次取出固定長度的陣列參考，長度不足時回傳 `None`。

範例：

```rust
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);
    }
}
```

輸出：

```text
[[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](https://github.com/rust-lang/rust/security/advisories/GHSA-q455-m56c-85mh) | std | `std::process::Command` did not properly escape arguments of batch files on Windows | 早於 1.77.2 | [1.77.2](/rust-1-77) |

### 仍影響 `1.77.2`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [CVE-2026-5222](https://github.com/rust-lang/cargo/security/advisories/GHSA-p688-r7jv-fm6f) | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | [1.96.0](/rust-1-96) |
| [CVE-2026-5223](https://github.com/rust-lang/cargo/security/advisories/GHSA-jq42-7mfv-hm57) | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | [1.96.0](/rust-1-96) |
| [CVE-2024-43402](https://github.com/rust-lang/rust/security/advisories/GHSA-2xg3-7mm6-98jj) | std | `std::process::Command` batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | [1.81.0](/rust-1-81) |

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

## 資料來源

- [Rust 1.77 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1770-2024-03-21)
- [Rust 1.77 發布公告](https://blog.rust-lang.org/releases/1.77.0/)
- [rust-lang/rust 原始碼 1.77.0](https://github.com/rust-lang/rust/tree/1.77.0)
