# Rust 1.97

Rust 1.97 發布於 2026-07-09，目前最新修補版為 `1.97.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.97 在穩定版預設改用 v0 符號修飾（symbol mangling）方案，泛型參數的實際值會保留在符號名稱中，不再只藏在雜湊後面。Cargo 新增 `build.warnings` 設定，統一控制警告要忽略、顯示或視為錯誤，取代在 CI 設 `RUSTFLAGS=-Dwarnings` 的做法。升級時要注意：舊版的除錯器或效能分析工具可能無法解讀 v0 符號，backtrace 的文字格式也可能改變；連結器的輸出預設會以警告顯示；`std::char` 模組的常數與函式被標記為棄用。

### 語言變更

- `must_use` lint 把 `Result<T, 無值型別>` 與 `ControlFlow<無值型別, T>` 視同 `T`。
- 新增預設關閉的 `dead_code_pub_in_binary` lint，找出 binary crate 中沒用到的 `pub` 項目。
- 穩定 `cfg(target_has_atomic_primitive_alignment)`。
- 更多情況允許在匯入路徑結尾寫 `self`，詳見[模組系統](/modules-system)。

### 標準庫重點

- 整數與 `NonZero` 整數新增 `bit_width`、`highest_one`、`lowest_one`、`isolate_highest_one`、`isolate_lowest_one`，詳見[數值型別](/pkg-num)。
- `RepeatN` 實作 `Default`，`ffi::FromBytesUntilNulError` 實作 `Copy`。
- `char::is_control` 可在 const 情境使用，詳見 [char](/pkg-char)。

### Cargo 與工具

- 穩定 `build.warnings` 設定：`allow` 隱藏警告、`warn`（預設）顯示、`deny` 讓建置失敗；改用這個設定不會讓建置快取失效，詳見 [Cargo](/modules-cargo)。
- 穩定 `resolver.lockfile-path`，指定解析相依時使用的 lockfile 路徑。
- `cargo clean` 在 `--target-dir` 看起來不像 Cargo 目標目錄時報錯，避免誤刪。
- 新增 `-m` 作為 `--manifest-path` 的縮寫。
- rustdoc 穩定 `--emit` 與 `--remap-path-prefix`。

### 相容性注意

- 預設使用 v0 符號修飾，舊版工具可能無法還原符號名稱。
- 連結器輸出預設以 `linker_messages` 警告顯示，可在 `Cargo.toml` 的 `[lints.rust]` 設為 `allow`。
- `std::char` 模組的常數與函式標記為棄用，詳見[棄用 API](/deprecations)。
- `pin!` 不再做解參考強制轉型：`pin!(x)` 在 `x` 是 `&mut T` 時一律得到 `Pin<&mut &mut T>`，詳見 [std::pin](/pkg-pin)。
- 移除自 1.0 起就已棄用的隱藏 `f64` 方法。
- Windows 上對已 `shutdown` 寫入端的 socket 寫入，錯誤種類改為 `BrokenPipe`，詳見 [std::net](/pkg-net)。

### 整數的位元查詢方法

處理旗標、位元遮罩或計算需要幾個位元時，常要自己組合 `leading_zeros` 與位移。新方法直接給答案：`bit_width` 是表示該數值需要的位元數；`highest_one`、`lowest_one` 回傳最高、最低的 1 所在的位元位置（數值為 0 時是 `None`）；`isolate_highest_one`、`isolate_lowest_one` 只保留最高或最低的那個 1。

範例：

```rust
fn main() {
    for n in [0u32, 1, 12, 40, 255] {
        println!(
            "{n:>3} = {n:08b}：bit_width {}，highest {:?}，lowest {:?}，isolate_highest {}，isolate_lowest {}",
            n.bit_width(),
            n.highest_one(),
            n.lowest_one(),
            n.isolate_highest_one(),
            n.isolate_lowest_one(),
        );
    }
}
```

輸出：

```text
  0 = 00000000：bit_width 0，highest None，lowest None，isolate_highest 0，isolate_lowest 0
  1 = 00000001：bit_width 1，highest Some(0)，lowest Some(0)，isolate_highest 1，isolate_lowest 1
 12 = 00001100：bit_width 4，highest Some(3)，lowest Some(2)，isolate_highest 8，isolate_lowest 4
 40 = 00101000：bit_width 6，highest Some(5)，lowest Some(3)，isolate_highest 32，isolate_lowest 8
255 = 11111111：bit_width 8，highest Some(7)，lowest Some(0)，isolate_highest 128，isolate_lowest 1
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.97.1` | 2026-07-16 | 否 | rustc: Fix miscompilation in LLVM optimization This backports an LLVM submodule bump to include the LLVM-side fix and a revert of the rustc change that is one known trigger for the bug. The rustc side revert should not be strictly necessary but is done out of abundance of caution. |

## 已知漏洞

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

## 資料來源

- [Rust 1.97 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1970-2026-07-09)
- [Rust 1.97 發布公告](https://blog.rust-lang.org/releases/1.97.0/)
- [rust-lang/rust 原始碼 1.97.0](https://github.com/rust-lang/rust/tree/1.97.0)
