# Rust 1.23

Rust 1.23 發布於 2018-01-04，目前最新修補版為 `1.23.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.23 是新年的第一個版本，變動不多：原本要 `use std::ascii::AsciiExt` 才能用的 ASCII 方法，直接搬到 `u8`、`char`、`[u8]`、`str` 上，不必再匯入特徵；`AsciiExt` 本身因此被標為棄用。編譯器避免了一些不必要的參數複製，rustc 的記憶體用量降低約 5%–10%。升級時要注意：rustdoc 會同時用舊的 Hoedown 與 CommonMark 兩種解析器渲染文件，結果不同時發出警告，為日後改用 CommonMark 預作準備。

### 語言變更

- 特徵物件可以附加任意 auto trait，例如 `Box<dyn Fn() + Send + Sync>`，詳見 [特徵物件](/traits-objects)。
- 二元運算的左側會套用子型別（subtyping），修正部分令人困惑的錯誤訊息。

### 標準庫重點

- `is_ascii`、`to_ascii_uppercase`、`eq_ignore_ascii_case`、`make_ascii_lowercase` 等方法直接定義在 `u8`、`char`、`[u8]`、`str` 上，詳見 [std::ascii](/pkg-ascii)。
- 原子型別實作 `From` 對應的一般型別，例如 `AtomicBool::from(true)`，詳見 [std::sync::atomic](/pkg-sync-atomic)。
- `()` 實作 `FromIterator<()>`，可以把一串 `Result<(), E>` 收集成單一個 `Result<(), E>`，詳見 [std::iter](/pkg-iter)。
- `RwLock<T>: Send` 不再要求 `T: Sync`，詳見 [std::sync](/pkg-sync)。
- `assert_eq!`、`assert_ne!` 允許結尾逗號；指向大小不固定型別的原始指標實作 `Hash`。

### Cargo 與工具

- `cargo check` 可以連同單元測試一起檢查，詳見 [單元測試](/testing-unit)。
- `cargo uninstall foo bar` 可以一次移除多個套件；`cargo install --version` 安裝指定版本。
- 發行版附上 Cargo book 文件，網址改為 doc.rust-lang.org/cargo。
- rustdoc 每次執行都會印出渲染差異的警告。

### 相容性注意

- 型別相等的判斷變得更正確，極少數程式碼可能無法編譯。
- `char::escape_debug` 改用 Unicode 10。
- Android SDK 升到 27、NDK 升到 r15c，最低支援 Android API 14；最低 LLVM 版本提高到 3.9。

### 直接定義在型別上的 ASCII 方法

處理 HTTP 方法、檔名副檔名這類只含英文的字串時，常需要「忽略大小寫比較」或「轉成大寫」。這些方法現在直接屬於 `str`、`char`、`u8` 與 `[u8]`，不用匯入任何特徵。它們只處理 ASCII 範圍，其他字元原樣保留。

範例：

```rust
fn main() {
    let cmd = "Get";
    println!("{}", cmd.eq_ignore_ascii_case("GET"));
    println!("{}", cmd.to_ascii_uppercase());

    for c in ['a', 'Z', '7', '中'] {
        println!("{:?} ascii={} 大寫={:?}", c, c.is_ascii(), c.to_ascii_uppercase());
    }

    let mut bytes = *b"hello";
    bytes.make_ascii_uppercase();
    println!("{:?}", std::str::from_utf8(&bytes));
    println!("{}", 97u8.is_ascii());
}
```

輸出：

```text
true
GET
'a' ascii=true 大寫='A'
'Z' ascii=true 大寫='Z'
'7' ascii=true 大寫='7'
'中' ascii=false 大寫='中'
Ok("HELLO")
true
```

注意：`std::ascii::AsciiExt` 已棄用，新程式碼不要再匯入它，詳見 [棄用 API](/deprecations)；需要完整 Unicode 大小寫轉換時用 `to_uppercase`。

### 把 Result<(), E> 收集成一個結果

對一串項目逐一驗證，每次回傳 `Result<(), E>`，最後只想知道「全部成功，或第一個錯誤是什麼」。因為 `()` 實作了 `FromIterator<()>`，可以直接 `collect` 成 `Result<(), E>`：遇到第一個 `Err` 就停止並回傳它。同一版的原子型別 `From` 轉換也一起示範。

範例：

```rust
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

fn check(age: i32) -> Result<(), String> {
    if age < 0 {
        Err(format!("年齡不可為負：{}", age))
    } else {
        println!("通過：{}", age);
        Ok(())
    }
}

fn main() {
    let all_ok: Result<(), String> = [18, 30].iter().map(|&a| check(a)).collect();
    println!("{:?}", all_ok);

    let some_bad: Result<(), String> = [5, -1, 40].iter().map(|&a| check(a)).collect();
    println!("{:?}", some_bad);

    let ready = AtomicBool::from(true);
    let hits = AtomicUsize::from(10);
    hits.fetch_add(1, Ordering::SeqCst);
    println!("{} {}", ready.load(Ordering::SeqCst), hits.load(Ordering::SeqCst));
}
```

輸出：

```text
通過：18
通過：30
Ok(())
通過：5
Err("年齡不可為負：-1")
true 11
```

注意：`collect` 遇到錯誤會立刻停止，`40` 沒有被檢查；需要收集所有錯誤時要自己走訪並存進 `Vec`。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.23.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) |
| [CVE-2021-28878](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28878) | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28879](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28879) | std | Zip can cause buffer overflow when a consumed Zip iterator is used again | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28876](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28876) | std | Panic safety issue in Zip specialization | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28875](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28875) | std | Logic bug in Read can cause buffer overflow in read_to_end() | 1.20.0 起，早於 1.50.0 | [1.50.0](/rust-1-50) |
| [CVE-2021-28877](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28877) | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起，早於 1.51.0 | [1.51.0](/rust-1-51) |
| [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) |
| [CVE-2019-16760](https://github.com/rust-lang/rust/security/advisories/GHSA-phjm-8x66-qw4r) | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | [1.26.0](/rust-1-26) |
| [CVE-2019-1010299](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-1010299) | std | vec_deque::Iter has unsound Debug implementation | 1.18.0 起，早於 1.30.0 | [1.30.0](/rust-1-30) |
| [CVE-2018-1000622](https://groups.google.com/forum/#!topic/rustlang-security-announcements/4ybxYLTtXuM) | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | [1.27.1](/rust-1-27) |
| [CVE-2018-25008](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-25008) | std | Insufficient synchronization in `Arc::get_mut` | 1.3.0 起，早於 1.29.0 | [1.29.0](/rust-1-29) |

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

## 資料來源

- [Rust 1.23 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1230-2018-01-04)
- [Rust 1.23 發布公告](https://blog.rust-lang.org/releases/1.23.0/)
- [rust-lang/rust 原始碼 1.23.0](https://github.com/rust-lang/rust/tree/1.23.0)
