# Rust 1.87

Rust 1.87 發布於 2025-05-15，目前最新修補版為 `1.87.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.87 在 Rust 1.0 發布滿十年當天推出，主要新功能是標準庫的匿名管線（anonymous pipe）`io::pipe`，以及 `Vec::extract_if`、`String::extend_from_within` 等實用 API。對新手來說，串接子行程的輸出入不再需要第三方 crate，從 `Vec` 中「挑出並移除」符合條件的元素也只要一行。升級時要注意：`i586-pc-windows-msvc` 目標已移除；許多 `std::arch` 內建函式改為可安全呼叫，既有程式可能出現新的 `unused_unsafe` 警告；格式化的 `width` 與 `precision` 上限改為 16 位元。

### 語言變更

- 穩定 `asm_goto`，行內組合語言可以跳到 Rust 程式碼區塊。
- 特徵中回傳位置的 `impl Trait` 也可以寫 `use<...>` 精確捕捉，詳見 [特徵進階](/traits-advanced)。
- 一元運算子 `!`、`-`、`*` 之後可以接開頭省略的區間 `..EXPR`。
- `assert_eq!`、`vec!` 等巨集支援 `const {...}` 運算式。

### 標準庫重點

- 新增 `io::pipe`、`io::PipeReader`、`io::PipeWriter`，可以轉成 `Stdio` 交給子行程，詳見 [std::io](/pkg-io) 與 [std::process](/pkg-process)。
- 新增 `Vec::extract_if` 與 `LinkedList::extract_if`，詳見 [Vec](/pkg-vec)。
- 切片新增 `split_off`、`split_off_first`、`split_off_last` 等方法，詳見 [切片](/pkg-slice)。
- 新增 `String::extend_from_within`，`String` 實作 `TryFrom<Vec<u8>>`，`str` 型別新增關聯函式 `str::from_utf8`、`str::from_utf8_mut` 等（以前只能從 `std::str` 模組呼叫），詳見 [String](/pkg-string)。
- 整數新增 `is_multiple_of`、`unbounded_shl`、`unbounded_shr`、`cast_signed`、`cast_unsigned` 與有號整數的 `midpoint`，詳見 [std::num](/pkg-num)。
- 新增 `OsStr::display` 與 `OsString::display`，詳見 [std::ffi](/pkg-ffi)。
- `Vec::with_capacity` 保證配置所要求的容量；`env::home_dir` 取消棄用，詳見 [std::env](/pkg-env)。

### Cargo 與工具

- Cargo 透過 ANSI OSC 9;4 序列在支援的終端機顯示進度，詳見 [Cargo 與套件](/modules-cargo)。
- `cargo package` 新增 `--exclude-lockfile` 旗標。
- 編譯器更新到 LLVM 20。

### 相容性注意

- 移除 `i586-pc-windows-msvc` 目標，請改用 `i686-pc-windows-msvc`。
- 原始指標的 `Debug` 輸出會附上指標的中繼資料（metadata）。
- `ControlFlow` 標註 `#[must_use]`，忽略回傳值會出現警告。
- 格式化參數的 `width` 與 `precision` 在所有目標上限制為 16 位元。
- Windows 上的標準庫不再連結 `advapi32`（win7 目標除外）。

### 匿名管線 io::pipe

`io::pipe()` 建立一對相連的端點：寫進 `PipeWriter` 的資料可以從 `PipeReader` 讀出。所有寫入端都關閉後，讀取端才會讀到結尾，所以範例先 `drop(writer)` 再 `read_to_string`。實務上常把兩端轉成 `Stdio`，讓子行程的 stdout 和 stderr 寫進同一條管線。

範例：

```rust
use std::io::{self, Read, Write};

fn main() -> io::Result<()> {
    let (mut reader, mut writer) = io::pipe()?;
    writer.write_all(b"hello from pipe")?;
    drop(writer);

    let mut text = String::new();
    reader.read_to_string(&mut text)?;
    println!("{text}");
    Ok(())
}
```

輸出：

```text
hello from pipe
```

注意：沒有關閉所有寫入端就呼叫 `read_to_string`，程式會一直等待而卡住；交給子行程使用時，要在 `wait()` 等它結束之前先讀取管線，否則輸出量大時作業系統的緩衝區會被塞滿。

### Vec::extract_if

`extract_if(範圍, 條件)` 走訪指定範圍內的元素，把條件成立的元素從 `Vec` 移除並逐一產生，其餘元素留在原地、順序不變。範圍用 `..` 代表整個 `Vec`。範例也示範 `is_multiple_of` 與 `cast_signed`：後者把 `u8` 的位元直接解讀為 `i8`，所以 `200` 變成 `-56`。

範例：

```rust
fn main() {
    let mut numbers = vec![1, 2, 3, 4, 5, 6, 7, 8];
    let evens: Vec<i32> = numbers.extract_if(.., |n| *n % 2 == 0).collect();
    println!("{:?}", evens);
    println!("{:?}", numbers);

    let mut tasks = vec!["a", "b", "c", "d", "e"];
    let late: Vec<&str> = tasks.extract_if(2.., |_| true).collect();
    println!("{:?} {:?}", tasks, late);

    println!("{} {}", 12u32.is_multiple_of(4), 10u32.is_multiple_of(4));
    println!("{}", 200u8.cast_signed());
}
```

輸出：

```text
[2, 4, 6, 8]
[1, 3, 5, 7]
["a", "b"] ["c", "d", "e"]
true false
-56
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.87.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-2025-11233](https://github.com/rust-lang/rust/security/advisories/GHSA-rv9c-3mvf-cw9h) | std | Rust standard library didn't detect all path separators on Cygwin | 1.87.0 起，早於 1.89.0 | [1.89.0](/rust-1-89) |

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

## 資料來源

- [Rust 1.87 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1870-2025-05-15)
- [Rust 1.87 發布公告](https://blog.rust-lang.org/releases/1.87.0/)
- [rust-lang/rust 原始碼 1.87.0](https://github.com/rust-lang/rust/tree/1.87.0)
