# Rust 1.32

Rust 1.32 發布於 2019-01-17，目前最新修補版為 `1.32.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.32 帶來新手最實用的除錯工具 `dbg!` 巨集，同時把程式預設的記憶體配置器從 jemalloc 改為系統配置器。2018 edition 的模組路徑規則也定案：沒有 `self`、`super`、`crate` 前綴的路徑，會先解析成目前模組內的項目。升級時要注意：如果依賴 jemalloc 的效能，需要自行加入 `jemallocator` crate 並以 `#[global_allocator]` 指定。

### 語言變更

- 2018 edition：巨集定義可以使用 `?` 重複運算子，表示出現零次或一次。
- 2018 edition：沒有前綴關鍵字的路徑會優先解析成模組內的項目，再找外部 crate 或 prelude，例如 `use Color::*;`，詳見 [模組系統](/modules-system)。
- `Self` 可以當作單元結構體與元組結構體的建構子與模式，也能用在型別定義裡，詳見 [方法](/structs-methods)。
- 巨集新增 `literal` 片段，比對任何型別的字面值，例如 `1`、`'A'`、`"Hello World"`，詳見 [宣告式巨集](/macros-declarative)。
- 可以對 `PhantomData<T>` 做模式比對；trait 可以標上 `#[must_use]`。

### 標準庫重點

- `dbg!` 巨集穩定，把運算式與值印到 stderr 並原樣回傳。
- 所有整數型別新增 `to_be_bytes`、`to_le_bytes`、`to_ne_bytes` 與 `from_be_bytes`、`from_le_bytes`、`from_ne_bytes`，詳見 [整數](/types-integers)。
- `PathBuf` 實作 `FromStr`；`Box<[T]>` 實作 `FromIterator<T>`。
- `Duration::as_secs`、`slice::as_ptr`、`iter::empty`、`Ipv6Addr::new` 等函式改為 `const fn`。

### Cargo 與工具

- `cargo c` 是 `cargo check` 的別名。
- 替代 registry 的 URL 可以包含使用者名稱。

### 相容性注意

- 程式預設改用系統配置器；編譯器本身在 Linux 與 macOS 上仍使用 jemalloc。
- AVX 的 `_mm256_stream_si256`、`_mm256_stream_pd`、`_mm256_stream_ps` 參數型別從 `*const` 改為 `*mut`，因為原本的實作不健全（unsound）。

### dbg! 巨集

`dbg!` 會印出「檔名、行號、欄號、運算式原文與值」，然後把值原樣回傳，所以可以直接包在運算式中間而不影響邏輯。它寫到 stderr 而不是 stdout，不會混進程式正式的輸出。範例的輸出區塊中，前兩行來自 stderr，`done` 來自 stdout。

範例：

```rust
fn area(w: u32, h: u32) -> u32 {
    dbg!(w * h)
}

fn main() {
    let w = 3;
    let total = area(w, 4) + 1;
    dbg!(total);
    println!("done");
}
```

輸出：

```text
[main.rs:2:5] w * h = 12
[main.rs:8:5] total = 13
done
```

注意：`dbg!` 會取得參數的所有權再回傳；不想移動值時傳參考，例如 `dbg!(&name)`。

### 整數與位元組陣列互轉

處理網路協定或檔案格式時，常要把整數拆成位元組或反過來組合。`to_be_bytes` 以大端序（big-endian，高位在前）輸出，`to_le_bytes` 以小端序輸出；`from_*_bytes` 則從固定長度的陣列組回整數。同一組位元組用不同的位元組序解讀，會得到完全不同的值。

範例：

```rust
fn main() {
    let n: u32 = 0x12345678;
    println!("{:02x?}", n.to_be_bytes());
    println!("{:02x?}", n.to_le_bytes());

    let bytes = [0x00, 0x00, 0x01, 0x00];
    println!("{}", u32::from_be_bytes(bytes));
    println!("{}", u32::from_le_bytes(bytes));
}
```

輸出：

```text
[12, 34, 56, 78]
[78, 56, 34, 12]
256
65536
```

### 以 Self 建構元組結構體

在 `impl` 區塊中，`Self` 現在可以當作元組結構體的建構子與模式，改名結構體時不必逐一修改方法內容。

範例：

```rust
#[derive(Debug)]
struct Point(i32, i32);

impl Point {
    fn origin() -> Self {
        Self(0, 0)
    }

    fn is_origin(&self) -> bool {
        match self {
            Self(0, 0) => true,
            _ => false,
        }
    }
}

fn main() {
    let p = Point::origin();
    println!("{:?} {}", p, p.is_origin());
    let q = Point(1, 2);
    println!("{:?} {}", q, q.is_origin());
}
```

輸出：

```text
Point(0, 0) true
Point(1, 2) false
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.32.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-2020-36323](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-36323) | std | API soundness issue in join() implementation of \[Borrow\<str\>\] | 1.28.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2020-36317](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-36317) | std | String::retain allows safely creating invalid strings when abusing panic | 1.26.0 起，早於 1.49.0 | [1.49.0](/rust-1-49) |
| [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.32 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1320-2019-01-17)
- [Rust 1.32 發布公告](https://blog.rust-lang.org/releases/1.32.0/)
- [rust-lang/rust 原始碼 1.32.0](https://github.com/rust-lang/rust/tree/1.32.0)
