# Rust 1.67

Rust 1.67 發布於 2023-01-26，目前最新修補版為 `1.67.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.67 是一個小而穩的版本：`async fn` 上的 `#[must_use]` 終於會作用在等待後的結果，`std::sync::mpsc` 的實作換成以 crossbeam-channel 為基礎，API 不變但修正多個錯誤並提升效能。標準庫新增整數對數方法 `ilog2`、`ilog10`、`ilog`。升級要注意：格式化浮點數到 0 位小數時，`0.5` 改為輸出 `0`（四捨六入五成雙），`&&`／`||` 鏈中暫時值的 drop 順序也改為由左到右。

### 語言變更

- `#[must_use]` 標在 `async fn` 上時，會作用在 `.await` 之後的輸出值，忽略時會警告，非同步見 [async 基礎](/async-basics)。
- `cfg(FALSE)` 下的無效字面值不再是錯誤。
- `Sized` 條件改為共歸納（coinductive），允許循環。

### 標準庫重點

- 整數新增 `ilog`、`ilog2`、`ilog10` 以及對應的 `checked_` 版本；`NonZeroU*` 新增 `ilog2`、`ilog10`，`NonZero*` 新增 `BITS` 常數，詳見 [std::num](/pkg-num)。
- `std::sync::mpsc` 改以 crossbeam-channel 實作，詳見 [std::sync::mpsc](/pkg-sync-mpsc) 與 [通道](/concurrency-channels)。
- `char::from_u32`、`char::from_digit`、`char::to_digit` 可在 const 環境使用，詳見 [char](/pkg-char)。
- `ControlFlow` 實作 `Eq` 與 `Hash`。

### Cargo 與工具

- 原生函式庫修飾子 `verbatim` 穩定。
- UEFI 目標（`x86_64-unknown-uefi` 等）升為 Tier 2。
- 設定中若有多個 registry 使用相同的 index URL，Cargo 會回報錯誤。

### 相容性注意

- `format!("{:.0}", 0.5)` 輸出 `0`，與其他位數一致採用「平手時取偶數」的捨入，格式化見 [std::fmt](/pkg-fmt)。
- `&&` 與 `||` 鏈中各子運算式的暫時值依求值順序由左到右 drop。
- 字串字面值後接底線後綴（如 `"x"_suffix`）成為硬性錯誤。
- `repr(Rust)` 型別的欄位排列調整，少數列舉會變大；`repr(Rust)` 的布局本來就不保證穩定。

### 整數對數 ilog2、ilog10、ilog

`ilog2` 回傳以 2 為底的對數（無條件捨去），也就是「最高位元的位置」；`ilog10` 是以 10 為底，可以拿來算十進位位數；`ilog(b)` 指定任意底數。對 `0` 取對數沒有意義，`ilog` 系列會 panic，`checked_ilog2` 等則回傳 `None`。

範例：

```rust
fn main() {
    let n: u32 = 1000;
    println!("{} {} {}", n.ilog2(), n.ilog10(), n.ilog(3));
    println!("{:?}", 0u32.checked_ilog2());
    for bytes in [1u64, 1023, 1024, 1_048_576] {
        println!("{bytes} 需要 {} 位元", bytes.ilog2() + 1);
    }
}
```

輸出：

```text
9 3 6
None
1 需要 1 位元
1023 需要 10 位元
1024 需要 11 位元
1048576 需要 21 位元
```

注意：`ilog2`、`ilog10`、`ilog` 在值小於等於 0 時會 panic；輸入可能為 0 時用 `checked_` 版本。

### 0.5 的格式化捨入

`{:.0}` 把浮點數格式化成整數字串。Rust 1.67 修正了 `0.5` 被捨入成 `1` 的不一致行為：現在所有平手的情況都捨入到偶數，`0.5` → `0`、`1.5` → `2`、`2.5` → `2`。要顯示給使用者看的金額或百分比，若需要「四捨五入」，請先用 `f64::round` 處理再格式化，見 [f64](/pkg-f64)。

範例：

```rust
fn main() {
    for x in [0.5f64, 1.5, 2.5, 3.5] {
        print!("{x:.0} ");
    }
    println!();
}
```

輸出：

```text
0 2 2 4 
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.67.1` | 2023-02-09 | 否 | Fix interoperability with thin archives.；Fix an internal error in the compiler build process.；Downgrade `clippy::uninlined_format_args` to pedantic. |

## 已知漏洞

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

### 仍影響 `1.67.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) |
| [CVE-2024-43402](https://github.com/rust-lang/rust/security/advisories/GHSA-2xg3-7mm6-98jj) | std | `std::process::Command` batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | [1.81.0](/rust-1-81) |
| [CVE-2024-24576](https://github.com/rust-lang/rust/security/advisories/GHSA-q455-m56c-85mh) | std | `std::process::Command` did not properly escape arguments of batch files on Windows | 早於 1.77.2 | [1.77.2](/rust-1-77) |
| [CVE-2023-40030](https://github.com/rust-lang/cargo/security/advisories/GHSA-wrrj-h57r-vx9p) | cargo | Malicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports | 1.60.0 起，早於 1.72.0 | [1.72.0](/rust-1-72) |
| [CVE-2023-38497](https://github.com/rust-lang/cargo/security/advisories/GHSA-j3xp-wfr4-hx87) | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | [1.71.1](/rust-1-71) |

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

## 資料來源

- [Rust 1.67 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1670-2023-01-26)
- [Rust 1.67 發布公告](https://blog.rust-lang.org/releases/1.67.0/)
- [rust-lang/rust 原始碼 1.67.0](https://github.com/rust-lang/rust/tree/1.67.0)
