# Rust 1.55

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

## 版本介紹

Rust 1.55 讓模式可以寫「只有起點」的開放範圍，例如 `1..` 代表 1 到該整數型別的最大值；陣列新增 `map`，可以直接把 `[T; N]` 轉成 `[U; N]`。標準庫的浮點數解析改用 Eisel-Lemire 演算法，更快也更正確，Cargo 會合併重複的編譯警告。升級時要注意：標準庫回傳的 `io::Error` 不再使用 `ErrorKind::Other`，比對錯誤種類時要保留 `_` 分支。這版獻給創造「turbofish」（`::<>`）一詞的社群成員 Anna Harren。

### 語言變更

- 模式可以使用開放的起點範圍 `X..`，詳見 [模式](/control-patterns)。
- 可以透過 `std::prelude` 明確匯入不同版次的 prelude，例如 `use std::prelude::rust_2021::*;`，詳見 [prelude](/pkg-prelude)。

### 標準庫重點

- 新增 `array::map`，詳見 [陣列](/pkg-array)。
- 新增 `ops::ControlFlow`，搭配 `try_for_each` 等方法提前結束走訪，詳見 [運算子特徵](/pkg-ops)。
- 浮點數字串解析改用 Eisel-Lemire 演算法，部分以前被拒絕的合法值也能解析，詳見 [f64](/pkg-f64)。
- 新增 `Bound::cloned`、`string::Drain::as_str`、`IntoInnerError::into_error`、`IntoInnerError::into_parts`。
- 新增 `MaybeUninit::write`、`assume_init_ref`、`assume_init_mut`，詳見 [std::mem](/pkg-mem)。

### Cargo 與工具

- 平行編譯同一個 crate 時（如 `cargo test`）不再重複印出相同的警告與錯誤，詳見 [Cargo](/modules-cargo)。
- 新增 `cargo d` 作為 `cargo doc` 的別名；`cargo tree` 可以用 `{lib}` 格式印出函式庫名稱。
- `cargo metadata` 的套件資訊包含 `default_run` 欄位。
- rustdoc 的特徵實作區塊預設展開，方便用瀏覽器搜尋。

### 相容性注意

- 標準庫回傳的 `io::Error` 不再使用 `ErrorKind::Other`，未分類的錯誤改用內部的 `Uncategorized`，詳見 [std::io](/pkg-io)。
- 建置腳本不再取得 `RUSTFLAGS`，改用 `CARGO_ENCODED_RUSTFLAGS`。
- Windows 上 `process::Command` 的環境變數名稱不再被轉成大寫。
- 沒有 `rustdoc::` 前綴的 rustdoc lint 會產生警告。

### 開放範圍模式

`1..` 在模式中代表「1 以上」，和 `0`、`1..=9` 這類模式一起使用就能窮舉整個 `u32`，不必再寫 `_` 兜底，觀念詳見 [match](/control-match)。

範例：

```rust
fn describe(n: u32) -> &'static str {
    match n {
        0 => "zero",
        1..=9 => "single digit",
        10..=99 => "two digits",
        100.. => "large",
    }
}

fn main() {
    for n in [0, 7, 42, 1000] {
        println!("{n}: {}", describe(n));
    }
}
```

輸出：

```text
0: zero
7: single digit
42: two digits
1000: large
```

注意：這版只開放 `X..`；`1..10` 這種不含終點的範圍模式要到 [Rust 1.80](/rust-1-80) 起才能使用。

### array::map

`arr.map(f)` 對每個元素套用 `f`，回傳長度相同的新陣列，不必先轉成 `Vec` 再轉回來。

範例：

```rust
fn main() {
    let celsius = [0.0, 25.0, 100.0];
    let fahrenheit = celsius.map(|c| c * 9.0 / 5.0 + 32.0);
    println!("{:?}", fahrenheit);

    let words = ["a", "bb", "ccc"];
    let lens: [usize; 3] = words.map(str::len);
    println!("{:?}", lens);
}
```

輸出：

```text
[32.0, 77.0, 212.0]
[1, 2, 3]
```

### ops::ControlFlow

`ControlFlow` 有 `Continue` 與 `Break` 兩個變體，用來明確表達「繼續」或「停下並帶回一個值」。搭配 `try_for_each`，遇到第一個負數就停止走訪。

範例：

```rust
use std::ops::ControlFlow;

fn main() {
    let data = [3, 8, -1, 5];
    let result = data.iter().try_for_each(|&x| {
        if x < 0 {
            return ControlFlow::Break(x);
        }
        println!("ok {x}");
        ControlFlow::Continue(())
    });
    println!("{:?}", result);
}
```

輸出：

```text
ok 3
ok 8
Break(-1)
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.55.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.55 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1550-2021-09-09)
- [Rust 1.55 發布公告](https://blog.rust-lang.org/releases/1.55.0/)
- [rust-lang/rust 原始碼 1.55.0](https://github.com/rust-lang/rust/tree/1.55.0)
