# 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](/lowlevel-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]`，詳見 [元組與陣列](/types-tuples-arrays)。
- `BuildHasher::hash_one` 一次算出某個值的雜湊，詳見 [std::hash](/pkg-hash)。
- `thread_local!` 的 `const { ... }` 初始化寫法（1.59 起已可用）補進文件，詳見 [std::thread](/pkg-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` 會自動繼承工作區欄位，見 [工作區](/modules-workspaces)。
- rustdoc 新增 `rustdoc::unescaped_backticks` lint。

### 相容性注意

- `*-linux-musl` 目標升級到 musl 1.2.3，32 位元系統啟用 time64。
- 遞迴 panic 處理改寫：unwind 過程中的額外 panic 只要在離開 `Drop` 前被捕捉就允許，但 panic hook 中的 panic 會立即中止，見 [panic](/errors-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](/pkg-convert)。

範例：

```rust
fn main() {
    let point: (i32, i32, i32) = [3, 4, 5].into();
    println!("{point:?}");

    let rgb: [u8; 3] = (255, 128, 0).into();
    println!("{rgb:?} {}", rgb.iter().map(|c| format!("{c:02x}")).collect::<String>());
}
```

輸出：

```text
(3, 4, 5)
[255, 128, 0] ff8000
```

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

### const 初始化的 thread_local

`thread_local!` 宣告「每條執行緒各有一份」的變數；初始值寫成 `const { ... }` 時，編譯器不必在每次存取時檢查是否已初始化，產生的程式碼更有效率。下面主執行緒加了兩次、另一條執行緒只加一次，兩者互不影響。

範例：

```rust
use std::cell::Cell;
use std::thread;

thread_local! {
    static COUNTER: Cell<u32> = const { Cell::new(0) };
}

fn bump() -> u32 {
    COUNTER.with(|c| {
        c.set(c.get() + 1);
        c.get()
    })
}

fn main() {
    bump();
    bump();
    let other = thread::spawn(|| bump()).join().unwrap();
    println!("main={} other={other}", COUNTER.with(|c| c.get()));
}
```

輸出：

```text
main=2 other=1
```

### BuildHasher::hash_one

`BuildHasher::hash_one` 省去「建立 hasher、餵資料、`finish`」三步。`&str` 與 `String` 的雜湊結果相同，這正是 `HashMap<String, _>` 能用 `&str` 查詢的原因。雜湊值本身每次執行都不同，所以範例只印比較結果。

範例：

```rust
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;

fn main() {
    let state = RandomState::new();
    let a = state.hash_one("rust");
    let b = state.hash_one(String::from("rust"));
    let c = state.hash_one("go");
    println!("{} {}", a == b, a == c);
}
```

輸出：

```text
true false
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.71.1` | 2023-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-38497](https://github.com/rust-lang/cargo/security/advisories/GHSA-j3xp-wfr4-hx87) | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | [1.71.1](/rust-1-71) |

### 仍影響 `1.71.1`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [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) |
| [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) |
| [CVE-2023-40030](https://github.com/rust-lang/cargo/security/advisories/GHSA-wrrj-h57r-vx9p) | cargo | Malicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports | 1.60.0 起，早於 1.72.0 | [1.72.0](/rust-1-72) |

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

## 資料來源

- [Rust 1.71 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1710-2023-07-13)
- [Rust 1.71 發布公告](https://blog.rust-lang.org/releases/1.71.0/)
- [rust-lang/rust 原始碼 1.71.0](https://github.com/rust-lang/rust/tree/1.71.0)
