# Rust 1.91

Rust 1.91 發布於 2025-10-30，目前最新修補版為 `1.91.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.91 把 `aarch64-pc-windows-msvc`（ARM64 Windows）升為 Tier 1，享有最高等級的測試保證。語言方面新增預設警告的 `dangling_pointers_from_locals` lint：函式回傳指向區域變數的原始指標時會被提醒。標準庫一口氣穩定大量 API，包括 `strict_*` 整數運算、`Path::file_prefix`、`Duration::from_mins` 等。升級時要注意：panic 訊息開始印出執行緒 ID，`semicolon_in_expressions_from_macros` 改為預設拒絕，屬性解析也變得更嚴格。

### 語言變更

- 新增 `dangling_pointers_from_locals` lint，警告回傳指向區域變數的[原始指標](/pkg-ptr)；回傳本身不是 unsafe，解參考才是。
- 新增預設警告的 `integer_to_ptr_transmutes` lint，針對把整數 transmute 成指標。
- 模式綁定依書寫順序處理，drop 順序以主要綁定的順序為準。
- `sysv64`、`win64`、`efiapi`、`aapcs` ABI 可宣告 C 風格可變參數函式。
- 新增 `target_env = "macabi"`、`target_env = "sim"` 兩個 cfg。

### 標準庫重點

- 整數新增 `strict_add`、`strict_sub`、`strict_mul`、`strict_pow` 等 `strict_*` 方法，溢位時一律 panic，詳見[數值型別](/pkg-num)。
- `Path::file_prefix`、`PathBuf::add_extension`、`PathBuf::with_added_extension`，以及 `Path`／`PathBuf` 與 `str`／`String` 的 `==` 比較，詳見 [std::path](/pkg-path)。
- `Duration::from_mins`、`Duration::from_hours`，詳見 [std::time](/pkg-time)。
- `str::floor_char_boundary`、`str::ceil_char_boundary` 找最近的字元邊界，詳見 [str](/pkg-str)。
- `PanicHookInfo::payload_as_str` 直接取得 panic 訊息字串，詳見 [std::panic](/pkg-panic)。
- `BTreeMap::extract_if`、`BTreeSet::extract_if`、`core::iter::chain`、`core::array::repeat`、`Ipv4Addr::from_octets` 與 `AtomicPtr` 的 `fetch_*` 系列。
- panic 訊息會印出執行緒 ID；設定執行緒堆疊大小失敗時改回傳錯誤，不再 panic，詳見 [std::thread](/pkg-thread)。

### Cargo 與工具

- 穩定 `build.build-dir` 設定，指定中間建置產物的存放目錄，詳見 [Cargo](/modules-cargo)。
- `--target` 與 `build.target` 可以寫 `"host-tuple"`，自動代換成本機的目標三元組。
- rustdoc 搜尋支援以原始指標型別查詢，例如 `*const u8 ->`。

### 相容性注意

- `semicolon_in_expressions_from_macros` 改為預設拒絕：表達式位置的 `macro_rules!` 巨集展開結果不能以分號結尾，詳見[宣告式巨集](/macros-declarative)。
- 屬性解析更嚴格，格式錯誤的 `#[should_panic]`、`#[link]` 等會直接報錯。
- 名稱解析中的棄用 lint 全部改為預設拒絕，並在相依套件中回報。
- 2024 edition 中，`if let` 條件裡由 `pin!`、`format_args!`、`write!`、`writeln!` 產生的暫存值作用範圍縮短。
- Apple 平台連結時一律傳入 SDK 根目錄，`/usr/local/lib` 的函式庫可能不再自動連結。
- 以 `static` 標記的閉包在語法上不再合法。

### strict 整數運算

Rust 的整數溢位在 debug 建置會 panic、release 建置預設會繞回，行為隨建置模式不同。`strict_*` 系列不論建置模式，溢位時一律 panic，適合「溢位就是程式錯誤」的場合。範例用 `panic::set_hook` 自訂 panic 訊息，並以 `catch_unwind` 接住 panic；`payload_as_str` 也是這版穩定的 API。

範例：

```rust
use std::panic;

fn main() {
    println!("{}", 200u8.strict_add(50));
    println!("{}", (-7i32).strict_abs());
    println!("{}", 3u32.strict_pow(4));

    panic::set_hook(Box::new(|info| {
        println!("panic: {}", info.payload_as_str().unwrap_or("(非字串)"));
    }));
    let result = panic::catch_unwind(|| 200u8.strict_add(100));
    println!("結果是 Err：{}", result.is_err());
}
```

輸出：

```text
250
7
81
panic: attempt to add with overflow
結果是 Err：true
```

注意：若要在溢位時改走其他流程而不是 panic，改用 `checked_*` 系列，它回傳 `Option`。

### Path::file_prefix 與 add_extension

`file_stem` 只拿掉最後一個副檔名，`report.tar.gz` 得到 `report.tar`；`file_prefix` 則取第一個 `.` 之前的部分，得到 `report`。`add_extension` 在既有副檔名後面再加一個，而不是取代；`with_added_extension` 是回傳新 `PathBuf` 的版本。

範例：

```rust
use std::path::{Path, PathBuf};

fn main() {
    let p = Path::new("backup/report.tar.gz");
    println!("file_stem   = {:?}", p.file_stem());
    println!("file_prefix = {:?}", p.file_prefix());

    let mut log = PathBuf::from("logs/app.log");
    log.add_extension("1");
    println!("{}", log.display());
    let zipped = log.with_added_extension("gz");
    println!("{}", zipped.display());

    println!("{}", Path::new("a/b") == "a/b");
}
```

輸出：

```text
file_stem   = Some("report.tar")
file_prefix = Some("report")
logs/app.log.1
logs/app.log.1.gz
true
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.91.1` | 2025-11-10 | 否 | Enable file locking support in illumos. This fixes Cargo not locking the build directory on illumos.；Fix `wasm_import_module` attribute cross-crate. This fixes linker errors on WASM targets. |

## 已知漏洞

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

### 仍影響 `1.91.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.91 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1910-2025-10-30)
- [Rust 1.91 發布公告](https://blog.rust-lang.org/releases/1.91.0/)
- [rust-lang/rust 原始碼 1.91.0](https://github.com/rust-lang/rust/tree/1.91.0)
