# Rust 1.52

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

## 版本介紹

Rust 1.52 最大的改變在工具：先跑 `cargo check` 再跑 `cargo clippy` 時，Clippy 以前會因為 Cargo 的建置快取而沒有真的執行，現在不論順序都會得到預期結果。標準庫新增常用的 `str::split_once`、`slice::partition_point`，以及一批 `char` 的關聯函式與常數。升級時要注意建置腳本不能再設定 `RUSTC_BOOTSTRAP`，並請直接使用本版的最新修補版（見下方「修補版本」）。

### 語言變更

- 新增 `unsafe_op_in_unsafe_fn` lint，檢查 `unsafe fn` 內的不安全操作是否包在 `unsafe` 區塊裡；預設為 allow，詳見 [unsafe](/lowlevel-unsafe)。
- 陣列的可變參考可以轉型成指向元素型別的原始指標。

### 標準庫重點

- 新增 `str::split_once`、`str::rsplit_once`，在第一個（最後一個）分隔符處把字串切成兩半，詳見 [str](/pkg-str)。
- 新增 `slice::partition_point`，在已排序切片中用二分搜尋找分界點，詳見 [切片](/pkg-slice)。
- 新增 `char::from_u32`、`char::from_digit`、`char::from_u32_unchecked`、`char::decode_utf16` 與常數 `char::MAX`、`char::REPLACEMENT_CHARACTER`、`char::UNICODE_VERSION`，詳見 [char](/pkg-char)。
- 新增 `Arguments::as_str`，詳見 [std::fmt](/pkg-fmt)。
- 所有整數除法與取餘數運算都是 `const`；`char::len_utf8`、`to_ascii_uppercase` 等方法成為 `const fn`。
- `OsString` 實作 `Extend` 與 `FromIterator`；`Arc<impl Error>` 實作 `Error`，詳見 [std::error](/pkg-error)。

### Cargo 與工具

- `cargo check` 與 `cargo clippy` 的快取不再互相干擾，詳見 [開發工具](/modules-tools)。
- `cargo test -- foo bar` 可以傳入多個測試過濾條件，詳見 [單元測試](/testing-unit)。
- rustdoc 的 lint 改為工具 lint，名稱加上 `rustdoc::` 前綴；文件 Markdown 支援工作清單 `- [x]`。
- 預設 LLVM 升級到 12。

### 相容性注意

- 建置腳本不能再設定 `RUSTC_BOOTSTRAP`。
- 移除 `x86_64-rumprun-netbsd` 目標；`x86_64-sun-solaris` 改用 `x86_64-pc-solaris`。
- rustdoc 指定程式碼區塊語言時只接受 `,`、空格與 tab 作為分隔符。
- 舊版 `proc-macro-hack` 可能因空白處理改變而 panic，執行 `cargo update` 即可修正。

### str::split_once

解析 `key=value` 這類字串時，`split_once('=')` 回傳 `Option<(&str, &str)>`：找到分隔符就切成前後兩段，找不到就是 `None`。`rsplit_once` 從右邊找。

範例：

```rust
fn main() {
    let line = "name=Rust=lang";
    println!("{:?}", line.split_once('='));
    println!("{:?}", line.rsplit_once('='));
    println!("{:?}", "no-equals".split_once('='));

    if let Some((key, value)) = "port: 8080".split_once(": ") {
        println!("{key} -> {value}");
    }
}
```

輸出：

```text
Some(("name", "Rust=lang"))
Some(("name=Rust", "lang"))
None
port -> 8080
```

### slice::partition_point

對一個「前段都符合條件、後段都不符合」的切片，`partition_point(條件)` 用二分搜尋回傳第一個不符合條件的索引。常用在已排序的 `Vec` 中找插入位置，讓插入後仍保持排序。

範例：

```rust
fn main() {
    let sorted = [1, 3, 5, 7, 9, 11];
    let idx = sorted.partition_point(|&x| x < 6);
    println!("{idx}");

    let mut v = vec![10, 20, 40, 50];
    let pos = v.partition_point(|&x| x < 30);
    v.insert(pos, 30);
    println!("{:?}", v);
}
```

輸出：

```text
3
[10, 20, 30, 40, 50]
```

### char 的關聯函式與常數

`char::from_digit(數字, 進位)` 把數字轉成對應的字元；`char::from_u32` 把 Unicode 碼位轉成 `char`，遇到不是合法字元的值（如代理區 `0xD800`）回傳 `None`。`char::MAX` 是最大的合法碼位，`REPLACEMENT_CHARACTER` 是解碼失敗時常用的替代字元 `U+FFFD`。

範例：

```rust
fn main() {
    println!("{:?}", char::from_digit(7, 10));
    println!("{:?}", char::from_digit(11, 16));
    println!("{:?}", char::from_u32(0x4E2D));
    println!("{:?}", char::from_u32(0xD800));
    println!("{:?}", char::MAX);
    println!("{}", char::REPLACEMENT_CHARACTER);
}
```

輸出：

```text
Some('7')
Some('b')
Some('中')
None
'\u{10ffff}'
�
```

## 本版新增的棄用 API

以下 API 從 Rust 1.52 起標記為棄用（deprecated），新程式不要再使用；說明欄為官方原文。

| 模組 | 項目 | 官方說明 |
| --- | --- | --- |
| `std::alloc` | [`LayoutErr`](https://doc.rust-lang.org/std/alloc/type.LayoutErr.html) | Name does not follow std convention, use LayoutError |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.52.1` | 2021-05-10 | 否 | - |

## 已知漏洞

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

### 仍影響 `1.52.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-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) |
| [CVE-2021-29922](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-29922) | std | Improper Input Validation of octal literals in `std::net` | 早於 1.53.0 | [1.53.0](/rust-1-53) |

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

## 資料來源

- [Rust 1.52 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1520-2021-05-06)
- [Rust 1.52 發布公告](https://blog.rust-lang.org/releases/1.52.0/)
- [rust-lang/rust 原始碼 1.52.0](https://github.com/rust-lang/rust/tree/1.52.0)
