# Rust 1.94

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

## 版本介紹

Rust 1.94 為切片新增 `array_windows`：和 `windows` 一樣產生滑動視窗，但每個視窗是固定長度的陣列 `&[T; N]`，長度甚至能從閉包的解構模式推斷出來。Cargo 開始支援在設定檔中用 `include` 載入其他設定檔，也改用 TOML 1.1 解析 `Cargo.toml`。升級時要注意：在 `Cargo.toml` 使用 TOML 1.1 新語法會提高開發時的最低 Rust 版本；標準庫巨集改由 prelude 匯入，自訂同名巨集再以 glob 匯入時可能出現名稱衝突。

### 語言變更

- impl 與 impl 項目繼承對應 trait 與 trait 項目的 `dead_code` lint 等級，詳見 [trait](/traits-basics)。
- 新增預設警告的 `unused_visibilities` lint，針對 `const _` 宣告上的可見性修飾。
- Unicode 更新到 17 版。
- 修正閉包中某些不正確的生命週期錯誤，詳見[閉包](/closures-basics)。

### 標準庫重點

- `<[T]>::array_windows` 產生固定長度的滑動視窗，詳見[切片](/pkg-slice)。
- `<[T]>::element_offset` 取得元素在切片中的索引。
- `LazyCell`、`LazyLock` 新增 `get`、`get_mut`、`force_mut`，可以查詢是否已初始化，詳見 [std::sync](/pkg-sync) 與 [std::cell](/pkg-cell)。
- `Peekable::next_if_map`、`next_if_map_mut`，詳見[迭代器](/pkg-iter)。
- 新增 `f32`／`f64` 的 `consts::EULER_GAMMA`、`consts::GOLDEN_RATIO`，以及 `impl TryFrom<char> for usize`，詳見 [f64](/pkg-f64)。
- `BinaryHeap<T>` 部分方法放寬 `T: Ord` 限制；`f32::mul_add`、`f64::mul_add` 可在 const 情境使用。

### Cargo 與工具

- 穩定設定檔的 `include` 鍵，`.cargo/config.toml` 可以載入其他設定檔，並可標記為 optional，詳見 [Cargo](/modules-cargo)。
- Cargo 改用 TOML v1.1 解析 manifest 與設定檔，例如 inline table 可以跨行並加結尾逗號。
- registry index 穩定 `pubtime` 欄位，記錄版本的發布時間。
- `CARGO_BIN_EXE_<crate>` 在執行期也能取得，詳見[整合測試](/testing-integration)。

### 相容性注意

- 標準庫巨集改由 prelude 匯入：自訂的同名巨集（例如 `matches`）以 glob 匯入時會產生歧義，需要明確匯入，詳見[宣告式巨集](/macros-declarative)。
- 閉包捕獲與模式比對的互動改得一致且正確，部分非 `move` 閉包可能改成只移動變數的一部分，造成新的借用錯誤或 `Drop` 時機改變。
- 運算式位置的 `include!` 不再去除 shebang 行。
- 跨 crate 的歧義 glob 重新匯出也會報錯。
- 生命週期識別字（如 `'a`）會做 NFC 正規化。
- Windows 上 `SystemTime::checked_sub_duration` 對 1601-01-01 之前的時間回傳 `None`。

### array_windows

`windows(2)` 產生的是長度不固定的切片 `&[T]`，要用 `w[0]`、`w[1]` 取值。`array_windows()` 產生 `&[T; N]`，可以直接在閉包參數寫 `[a, b]` 解構，編譯器從模式推斷出 `N = 2`；範例第二段寫 `[a, b, c]`，`N` 就是 3。

範例：

```rust
fn main() {
    let temps = [18, 21, 25, 24, 30];
    for [a, b] in temps.array_windows() {
        println!("{a} -> {b}：{:+}", b - a);
    }

    let rising = temps
        .array_windows()
        .filter(|[a, b, c]| a < b && b < c)
        .count();
    println!("連續三天上升的區段：{rising}");
}
```

輸出：

```text
18 -> 21：+3
21 -> 25：+4
25 -> 24：-1
24 -> 30：+6
連續三天上升的區段：1
```

### Peekable::next_if_map

`Peekable` 是可以「偷看」下一個元素的[迭代器](/iterators-basics)。`next_if_map` 把下一個元素交給閉包：閉包回傳 `Ok(值)` 就消耗該元素並回傳 `Some(值)`；回傳 `Err(原元素)` 就把元素放回去、回傳 `None`。適合「一邊解析一邊轉換，遇到不符合的就停下」。

範例：

```rust
fn main() {
    let tokens = ["12", "7", "x", "3"];
    let mut iter = tokens.iter().peekable();

    let mut numbers = Vec::new();
    while let Some(n) = iter.next_if_map(|s| s.parse::<i32>().map_err(|_| s)) {
        numbers.push(n);
    }
    println!("數字：{numbers:?}");
    println!("停在：{:?}", iter.next());
}
```

輸出：

```text
數字：[12, 7]
停在：Some("x")
```

### LazyLock::get

`LazyLock` 讓全域變數在第一次使用時才初始化。新的 `LazyLock::get` 在不觸發初始化的情況下查詢目前狀態：還沒初始化回傳 `None`，已初始化回傳 `Some(&值)`。範例中「初始化設定」只印一次，且出現在第一次讀取 `CONFIG.len()` 時。

範例：

```rust
use std::sync::LazyLock;

static CONFIG: LazyLock<Vec<&str>> = LazyLock::new(|| {
    println!("初始化設定");
    vec!["debug", "color"]
});

fn main() {
    println!("初始化前：{:?}", LazyLock::get(&CONFIG));
    println!("長度：{}", CONFIG.len());
    println!("初始化後：{:?}", LazyLock::get(&CONFIG));
}
```

輸出：

```text
初始化前：None
初始化設定
長度：2
初始化後：Some(["debug", "color"])
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.94.1` | 2026-03-26 | 是 | Fix `std::thread::spawn` on wasm32-wasip1-threads；Remove new methods added to `std::os::windows::fs::OpenOptionsExt` The new methods were unstable, but the trait itself is not sealed and so cannot be extended with non-default methods.；Clippy: fix ICE in `match_same_arms`；Cargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details. |

## 已知漏洞

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

### 本版已修正

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [CVE-2026-33055](https://www.cve.org/CVERecord?id=CVE-2026-33055) | cargo | Cargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details. | 未公布 | [1.94.1](/rust-1-94) |
| [CVE-2026-33056](https://www.cve.org/CVERecord?id=CVE-2026-33056) | cargo | Cargo: update tar to 0.4.45 This resolves CVE-2026-33055 and CVE-2026-33056. Users of crates.io are not affected. See blog for more details. | 未公布 | [1.94.1](/rust-1-94) |

### 仍影響 `1.94.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) |

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

## 資料來源

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