# Rust 1.100

Rust 1.100 發布於 2026-11-12，目前最新修補版為 `1.100.1`，**目前的最新穩定版**。

## 版本介紹

Rust 1.100 歷經 10 年、5 次失敗嘗試後，終於穩定永不型別（never type）`!`：它代表「永遠不會產生值」的計算，現在可以寫在任何型別位置，例如 `Result<T, !>`，而 `Infallible` 也改成 `!` 的別名。同一版穩定自訂配置器（Allocator API）的核心部分，以及在函式每條離開路徑都會執行清理的 `DropGuard`；Cargo 則新增未使用相依的警告與「最短發布時間」（dependency cooldowns）設定。升級時要注意：2024 之前的 edition 也把永不型別的 fallback 從 `()` 改為 `!`，少數程式碼可能受影響，若是相依套件報錯，先試著更新它；Cargo 的中間建置目錄改用新的結構。

### 語言變更

- 穩定永不型別 `!`，可在任何位置當型別使用；所有 edition 的 never type fallback 都改為 `!`，詳見[進階 trait](/traits-advanced)與[錯誤處理](/errors-result)。
- 穩定 `extern "custom"`，詳見 [FFI](/lowlevel-ffi)。
- 重複的 `#[repr(..)]` 屬性會觸發 lint；`repr(packed)` 中帶 `repr(align)` 欄位改為預設拒絕的 `aligned_fields_in_packed` lint。
- `dropping_references`、`dropping_copy_types`、`undropped_manually_drops` lint 擴及 `drop_in_place`。

### 標準庫重點

- 穩定 `std::alloc::Allocator` trait、`Global`、`System`，以及 `Box::new_in`、`Vec::new_in`、`Vec::with_capacity_in` 等帶配置器的 API，詳見 [Box](/pkg-boxed) 與 [Vec](/pkg-vec)。
- `core::mem::DropGuard` 在離開作用域時執行閉包，`core::mem::conjure_zst` 以 unsafe 方式建立零大小型別的值，詳見 [std::mem](/pkg-mem)。
- `str::trim_prefix`、`str::trim_suffix`、`[T]::trim_prefix`、`[T]::trim_suffix`、`Path::trim_prefix`，詳見 [str](/pkg-str)。
- `bool::toggle`、`Vec::from_fn`、`Box::take`、`Box::map`、`Rc::map`、`Arc::map`，詳見 [Rc](/pkg-rc)。
- `UnsafeCell::replace`、`Child::main_thread_handle`、`impl Default for std::sync::Once`；`ENOTSUP` 對應到 `ErrorKind::Unsupported`，詳見 [std::io](/pkg-io)。
- 標準庫改用 Unicode 18.0.0。

### Cargo 與工具

- Cargo 推出自己的 lint 系統，以 `Cargo.toml` 的 `[lints.cargo]` 控制，內建 `cargo::unused_dependencies` 檢查未使用的相依，詳見 [Cargo](/modules-cargo)。
- 穩定 min-publish-age：在 `.cargo/config.toml` 設定 `[registry] global-min-publish-age = "3 days"`，解析相依時略過太新發布的版本，降低供應鏈攻擊風險。
- `cargo install` 預設使用 `.crate` 內附的 lockfile，`--locked` 變成無作用。
- 中間建置產物改用新的目錄結構，依建置步驟而非檔案類型組織，減少快取衝突；依賴舊結構的工具應改用 Cargo 的 JSON 訊息。
- 測試框架允許重複傳入 `--test-threads` 與 `--color`，以後面的為準；rustdoc 新增 `invalid_markdown_table` lint。

### 相容性注意

- `Infallible` 改為 `!` 的型別別名，詳見 [std::convert](/pkg-convert)。
- 永不型別 fallback 在所有 edition 改為 `!`，編譯器可能判定更多程式碼不可達。
- 閉包對 `if let` 中使用的變數捕獲得更精確，可能產生新的錯誤，詳見[閉包](/closures-basics)。
- `box` 模式在語法上不再允許；`str::escape_debug` 一律跳脫字素延伸字元。
- `Iterator::{min,max}(_by_key)` 改用不同的 `Ord` 方法，`Ord` 實作不一致的型別結果可能改變，詳見 [std::cmp](/pkg-cmp)。
- 部分 `Box` 的實作推廣到任意配置器，可能出現新的型別推斷錯誤。

### 永不型別 !

`!` 是「不可能有值」的型別：`return`、`break`、`panic!`、`std::process::exit` 這類運算式的型別都是 `!`。因為它永遠不會真的產生值，`!` 可以自動轉成任何型別，所以 `match` 的一個分支寫 `return` 也能通過型別檢查。現在 `!` 還能寫在泛型參數裡：`Result<u32, !>` 表示「一定是 `Ok`」，用 `let Ok(v) = res;` 就能直接取值，不需要處理不存在的 `Err`。

範例：

```rust
use std::convert::Infallible;

fn always_ok(res: Result<u32, !>) -> u32 {
    let Ok(v) = res;
    v
}

fn parse_or_zero(s: &str) -> u32 {
    match s.parse() {
        Ok(n) => n,
        Err(_) => return 0,
    }
}

fn main() {
    println!("{}", always_ok(Ok(7)));
    let r: Result<u32, Infallible> = Ok(8);
    println!("{}", always_ok(r));
    println!("{} {}", parse_or_zero("42"), parse_or_zero("x"));
}
```

輸出：

```text
7
8
42 0
```

注意：`Infallible` 現在就是 `!`，所以範例中 `Result<u32, Infallible>` 可以直接傳給接收 `Result<u32, !>` 的函式。

### DropGuard

[Drop](/smart-pointers-overview) 讓型別在離開作用域時自動清理，但只為了「函式結束時做一件事」就定義新型別並不方便。`DropGuard::new(值, 閉包)` 包住一個值，可以像原本的值一樣使用（實作 `Deref`／`DerefMut`）；不論函式是正常返回、`?` 提早返回或 panic，guard 被丟棄時都會把值交給閉包。呼叫 `DropGuard::dismiss` 會取回值並取消閉包。範例利用它實作「全部成功才保留，任一失敗就還原」。

範例：

```rust
use std::mem::DropGuard;
use std::num::ParseIntError;

fn parse_all(nums: &mut Vec<i32>, inputs: &[&str]) -> Result<(), ParseIntError> {
    let len = nums.len();
    let mut nums = DropGuard::new(nums, move |nums| {
        nums.truncate(len);
    });
    for s in inputs {
        nums.push(s.parse()?);
    }
    DropGuard::dismiss(nums);
    Ok(())
}

fn main() {
    let mut nums = vec![1];
    println!("{:?} {nums:?}", parse_all(&mut nums, &["2", "3"]));
    println!("{:?} {nums:?}", parse_all(&mut nums, &["4", "x", "5"]));

    {
        let _log = DropGuard::new("任務", |name| println!("{name} 結束，執行清理"));
        println!("任務進行中");
    }
}
```

輸出：

```text
Ok(()) [1, 2, 3]
Err(ParseIntError { kind: InvalidDigit }) [1, 2, 3]
任務進行中
任務 結束，執行清理
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.100.1` | 2026-11-27 | 否 | library: fix regression where some types were not inferred as having `Iterator` impls；This backports a change to the unstable `const` version of `Iterator`, which unintentionally caused existing code to not be seen as implementing `Iterator` when a valid implementation was present. |

## 已知漏洞

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

## 資料來源

- [Rust 1.100 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-11000-2026-11-12)
- [Rust 1.100 發布公告](https://blog.rust-lang.org/releases/1.100.0/)
- [rust-lang/rust 原始碼 1.100.0](https://github.com/rust-lang/rust/tree/1.100.0)
