# Rust 1.96

Rust 1.96 發布於 2026-05-28，目前最新修補版為 `1.96.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.96 新增 `assert_matches!` 與 `debug_assert_matches!`：檢查值是否符合某個模式，失敗時會印出值的 `Debug` 內容，比 `assert!(matches!(..))` 好除錯得多。同一版穩定新的 `core::range::Range` 等範圍型別，它們實作 `IntoIterator` 而非 `Iterator`，因此可以是 `Copy`。升級時要注意：`assert_matches!` 沒有放進 prelude，使用前要從 `std` 或 `core` 匯入；WebAssembly 目標不再預設傳 `--allow-undefined` 給連結器，未定義的符號會變成連結錯誤。

### 語言變更

- `cfg` 可以接收 `expr` 類型的巨集變數，詳見[宣告式巨集](/macros-declarative)。
- tuple 運算式中的永不型別一律強制轉型。
- 允許 `ManuallyDrop` 型別的常數當作模式（修正 1.94 引入的回歸）。
- 支援 s390x 向量暫存器的內嵌組合語言。

### 標準庫重點

- `assert_matches!`、`debug_assert_matches!` 斷言值符合模式，詳見[單元測試](/testing-unit)。
- `core::range::Range`、`RangeFrom`、`RangeToInclusive` 與對應的迭代器型別，詳見 [std::ops](/pkg-ops)。
- `NonZero` 整數的範圍可以迭代，詳見[數值型別](/pkg-num)。
- `AssertUnwindSafe<T>`、`LazyCell<T, F>`、`LazyLock<T, F>` 實作 `From<T>`。

### Cargo 與工具

- 相依可以同時指定 git 倉庫與替代 registry：本機使用 git，發布時使用 registry 版本，詳見 [Cargo](/modules-cargo)。
- 設定檔支援 `target.'cfg(..)'.rustdocflags`。
- 修正 CVE-2026-5222 與 CVE-2026-5223。
- rustdoc 側邊欄把方法與關聯函式分開列出，棄用說明改用一般文件的方式呈現。

### 相容性注意

- WebAssembly 目標不再傳 `--allow-undefined`，需要舊行為時用 `RUSTFLAGS=-Clink-arg=--allow-undefined` 或 `#[link(wasm_import_module = "env")]`。
- `use S::{self as Other};` 這類對 struct 的 `{self}` 匯入不再允許，`{self}` 必須以模組為父路徑，詳見[模組系統](/modules-system)。
- `uninhabited_static` lint 改為預設拒絕並在相依套件中回報。
- 移除 `-Csoft-float`；外部 LLVM 最低版本提高到 21。
- `BTreeMap::append()` 經過最佳化，`Ord` 實作不正確的型別可能因此 panic。

### assert_matches!

`assert_matches!(值, 模式)` 檢查值是否符合模式，也可以加上 `if` 守衛。失敗時 panic 訊息會同時列出實際的值與期望的模式。範例以 `panic::set_hook` 自訂輸出，只印出 panic 訊息本身。

範例：

```rust
use std::assert_matches;

#[derive(Debug)]
enum Status {
    Ready,
    Running(u32),
}

fn main() {
    assert_matches!(Status::Running(3), Status::Running(1..=5));
    assert_matches!(Status::Ready, Status::Ready);
    assert_matches!(Some(42), Some(n) if n % 2 == 0);
    println!("全部斷言通過");

    std::panic::set_hook(Box::new(|info| {
        println!("{}", info.payload_as_str().unwrap_or("?"));
    }));
    let r = std::panic::catch_unwind(|| {
        assert_matches!(Status::Running(9), Status::Running(1..=5));
    });
    println!("失敗：{}", r.is_err());
}
```

輸出：

```text
全部斷言通過
assertion `left matches right` failed
  left: Running(9)
 right: Status::Running(1..=5)
失敗：true
```

注意：這兩個巨集不在 prelude 中，因為會與熱門第三方 crate 的同名巨集衝突，必須先 `use std::assert_matches;`（或 `core::assert_matches`）。

### 新的 Range 型別

`0..10` 產生的舊型別 `std::ops::Range` 本身就是迭代器，因此不能是 `Copy`，放進結構體後整個結構體也不能 `Copy`。新的 `std::range::Range` 只實作 `IntoIterator`，可以 `Copy`，欄位 `start`、`end` 公開，也能直接拿來索引字串與切片；要走訪時呼叫 `into_iter()`。目前 `a..b` 語法仍產生舊型別，用 `Range::from` 轉換。

範例：

```rust
use std::range::Range;

#[derive(Debug, Clone, Copy)]
struct Span(Range<usize>);

impl Span {
    fn of(self, s: &str) -> &str {
        &s[self.0]
    }
}

fn main() {
    let text = "hello world";
    let word = Span(Range::from(6..11));
    let copy = word;
    println!("{} / {}", word.of(text), copy.of(text));
    println!("start={} end={}", word.0.start, word.0.end);

    let total: usize = word.0.into_iter().sum();
    println!("6 到 10 加總 {total}");
}
```

輸出：

```text
world / world
start=6 end=11
6 到 10 加總 40
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.96.1` | 2026-06-30 | 是 | Cargo: fix timeout/retry behavior；Cargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2；rustc: fix miscompilation in MIR optimization |

## 已知漏洞

影響 `1.96.0` 的已知漏洞共 0 個；在本版修正的共 3 個；仍影響最新修補版 `1.96.1` 的共 0 個。官方未公布影響範圍的漏洞只列在修正它的版本，不計入其他版本。

### 本版已修正

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [CVE-2025-15661](https://www.cve.org/CVERecord?id=CVE-2025-15661) | cargo | Cargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2 | 未公布 | [1.96.1](/rust-1-96) |
| [CVE-2026-55199](https://www.cve.org/CVERecord?id=CVE-2026-55199) | cargo | Cargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2 | 未公布 | [1.96.1](/rust-1-96) |
| [CVE-2026-55200](https://www.cve.org/CVERecord?id=CVE-2026-55200) | cargo | Cargo: apply patches for CVE-2025-15661, CVE-2026-55199, and CVE-2026-55200 to libssh2 | 未公布 | [1.96.1](/rust-1-96) |

## 資料來源

- [Rust 1.96 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1960-2026-05-28)
- [Rust 1.96 發布公告](https://blog.rust-lang.org/releases/1.96.0/)
- [rust-lang/rust 原始碼 1.96.0](https://github.com/rust-lang/rust/tree/1.96.0)
