# Rust 1.54

Rust 1.54 發布於 2021-07-29，目前最新修補版為 `1.54.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.54 讓內建屬性的值可以呼叫函式型巨集，最常見的用法是 `#![doc = include_str!("README.md")]`，直接把 README 當成 crate 文件。WebAssembly 的 `wasm32` 內建函式（含 SIMD）也正式穩定。對新手來說，最直接的好處是 1.52.1 關掉的增量編譯（incremental compilation）在這版重新預設開啟，修改後重新編譯又變快了。升級時要注意閉包中混用 `Option` 與 `Result` 的 `?` 在型別推斷時不再被接受。

### 語言變更

- 部分內建屬性的值可以使用巨集，例如 `#[doc = concat!(...)]`、`#![doc = include_str!("README.md")]`，詳見 [註解與文件](/basics-comments-docs)。
- `const fn` 中可以在不定大小的切片型別之間轉型。
- `impl Trait` 可以帶多個彼此沒有存活關係的生命週期，詳見 [生命週期](/ownership-lifetimes)。

### 標準庫重點

- 新增 `HashMap::into_keys`、`HashMap::into_values`、`BTreeMap::into_keys`、`BTreeMap::into_values`，詳見 [HashMap](/pkg-collections-hash-map) 與 [BTreeMap](/pkg-collections-btree-map)。
- 新增 `VecDeque::binary_search`、`binary_search_by`、`binary_search_by_key` 與 `partition_point`，詳見 [集合](/pkg-collections)。
- 穩定 `arch::wasm32` 內建函式；`io::ErrorKind` 新增 `OutOfMemory`，詳見 [std::io](/pkg-io)。
- `panic::panic_any` 加上 `#[track_caller]`，詳見 [std::panic](/pkg-panic)。

### Cargo 與工具

- 增量編譯重新預設開啟。
- `cargo tree` 新增 `--prune`、`--depth`，以及 `--edges no-proc-macro`，詳見 [開發工具](/modules-tools)。
- 新增環境變數 `CARGO_TARGET_TMPDIR`，提供整合測試與基準測試存放暫存檔的目錄，詳見 [整合測試](/testing-integration)。
- rustfmt 會正確格式化巢狀的外部模組檔案，先前被忽略的檔案可能出現格式變更。

### 相容性注意

- 型別需要推斷時，閉包中不再允許透過 `?` 混用 `Option` 與 `Result`。
- 不同分支建構子需要不同生命週期的不健全程式碼不再被接受。
- `std::arch` 內建函式的常數參數檢查更嚴格，部分舊程式碼可能被拒絕。

### HashMap 與 BTreeMap 的 into_keys／into_values

`into_keys()`、`into_values()` 會消耗整個映射，直接取得鍵或值的所有權，不必再寫 `into_iter().map(|(k, _)| k)`。範例用 `BTreeMap` 讓輸出依鍵排序。

範例：

```rust
use std::collections::BTreeMap;

fn main() {
    let mut stock = BTreeMap::new();
    stock.insert("apple", 3);
    stock.insert("banana", 5);

    let names: Vec<&str> = stock.clone().into_keys().collect();
    let counts: Vec<i32> = stock.into_values().collect();
    println!("{:?}", names);
    println!("{:?}", counts);
}
```

輸出：

```text
["apple", "banana"]
[3, 5]
```

注意：`HashMap` 的 `into_keys`／`into_values` 順序不固定，需要穩定順序時先排序。

### VecDeque 的二分搜尋

`VecDeque` 和切片一樣可以對已排序的內容做二分搜尋：找到回傳 `Ok(索引)`，找不到回傳 `Err(應插入的索引)`。`partition_point` 則回傳第一個不符合條件的位置。

範例：

```rust
use std::collections::VecDeque;

fn main() {
    let mut q: VecDeque<i32> = VecDeque::from(vec![10, 20, 30, 40]);
    q.push_front(5);
    println!("{:?}", q.binary_search(&30));
    println!("{:?}", q.binary_search(&25));

    let pos = q.partition_point(|&x| x < 25);
    q.insert(pos, 25);
    println!("{:?}", q);
}
```

輸出：

```text
Ok(3)
Err(3)
[5, 10, 20, 25, 30, 40]
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.54.0`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [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-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) |
| [CVE-2022-46176](https://github.com/rust-lang/cargo/security/advisories/GHSA-r5w3-xm58-jv6j) | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | [1.66.1](/rust-1-66) |
| [CVE-2022-36113](https://github.com/rust-lang/cargo/security/advisories/GHSA-rfj2-q3h3-hm5j) | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | [1.64.0](/rust-1-64) |
| [CVE-2022-36114](https://github.com/rust-lang/cargo/security/advisories/GHSA-2hvr-h6gw-qrxp) | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | [1.64.0](/rust-1-64) |
| [CVE-2022-21658](https://github.com/rust-lang/rust/security/advisories/GHSA-r9cc-f5pr-p3j2) | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | [1.58.1](/rust-1-58) |
| [CVE-2021-42574](https://github.com/rust-lang/rust/security/advisories/GHSA-rcv6-wg5m-24v6) | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | [1.56.1](/rust-1-56) |

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

## 資料來源

- [Rust 1.54 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1540-2021-07-29)
- [Rust 1.54 發布公告](https://blog.rust-lang.org/releases/1.54.0/)
- [rust-lang/rust 原始碼 1.54.0](https://github.com/rust-lang/rust/tree/1.54.0)
