# Rust 1.37

Rust 1.37 發布於 2019-08-15，目前最新修補版為 `1.37.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.37 讓列舉成員可以透過型別別名存取，並新增名稱為 `_` 的匿名常數，方便巨集產生不會撞名的項目。Cargo 內建了 `cargo vendor`，可以把所有相依的原始碼下載到專案的 `vendor/` 目錄；`rustc` 也支援以 `-C profile-generate`、`-C profile-use` 進行剖析導引最佳化（PGO）。升級時要注意：範圍模式使用 `...` 與 trait 物件省略 `dyn` 從這版起預設產生警告。

### 語言變更

- 可以透過型別別名使用列舉成員，例如 `type MyOption = Option<u8>;` 之後寫 `MyOption::Some(y)`，詳見 [列舉](/enums-basics)。
- 常數可以用 `_` 命名，例如 `const _: u32 = 5;`，同一個作用域可以有多個。
- `#[repr(align(N))]` 可以用在列舉上。
- `cfg` 與 `cfg_attr` 可以用在泛型參數上。
- `#[must_use]` 型別被包在元組、`Box` 或陣列中而未使用時也會警告。
- 巨集的 `?` 重複運算子在 2015 edition 也能使用。

### 標準庫重點

- `Option::xor`：兩者恰好一個是 `Some` 時回傳它，否則回傳 `None`，詳見 [Option](/pkg-option)。
- `slice::copy_within` 在同一個切片內複製一段元素，詳見 [切片](/pkg-slice)。
- 所有整數與 `Wrapping` 新增 `reverse_bits`，反轉位元順序。
- `BufReader::buffer`、`BufWriter::buffer` 查看緩衝區內容，詳見 [std::io](/pkg-io)。
- `Cell::from_mut`、`Cell<[T]>::as_slice_of_cells` 與 `DoubleEndedIterator::nth_back` 穩定。
- `mem::MaybeUninit<T>` 與 `T` 的 ABI 相容。

### Cargo 與工具

- 內建 `cargo vendor`，把相依下載到 `vendor/` 並印出建置時使用它們的設定。
- 在 `[package]` 寫 `default-run = "foo"` 可以指定 `cargo run` 預設執行的二進位檔，詳見 [Cargo 專案](/modules-cargo)。
- 發佈含可執行檔的 crate 時，預設會一併包含 `Cargo.lock`。

### 相容性注意

- 包含範圍模式使用 `...` 會預設警告，請改用 `..=`。
- trait 物件沒有寫 `dyn` 會預設警告，請改寫成 `dyn Trait`，詳見 [trait 物件](/traits-objects)。

### 透過型別別名使用列舉成員

為很長的泛型型別取別名很常見，例如 `type Reply = Result<u32, String>;`。以前別名只能用在型別位置，建構或比對成員時仍要寫回 `Result::Ok`；現在可以直接寫 `Reply::Ok`、`Reply::Err`，讓程式碼與別名一致。

範例：

```rust
type Reply = Result<u32, String>;

fn show(r: Reply) -> String {
    match r {
        Reply::Ok(n) => format!("成功 {}", n),
        Reply::Err(e) => format!("失敗 {}", e),
    }
}

fn main() {
    println!("{}", show(Reply::Ok(7)));
    println!("{}", show(Reply::Err(String::from("timeout"))));
}
```

輸出：

```text
成功 7
失敗 timeout
```

### 匿名常數與其他新 API

`const _` 宣告一個不需要名字的常數，常見用途是在編譯期做檢查：範例中如果 `u64` 不是 8 個位元組，程式根本無法編譯。範例也示範 `Option::xor`、`copy_within`（把索引 0..2 複製到索引 3 開始的位置）與 `reverse_bits`。

範例：

```rust
const _: () = assert!(std::mem::size_of::<u64>() == 8);

fn main() {
    println!("{:?}", Some(1).xor(None::<i32>));
    println!("{:?}", Some(1).xor(Some(2)));

    let mut buf = [1, 2, 3, 4, 5];
    buf.copy_within(0..2, 3);
    println!("{:?}", buf);

    println!("{:08b}", 0b0000_0110u8.reverse_bits());
}
```

輸出：

```text
Some(1)
None
[1, 2, 3, 1, 2]
01100000
```

注意：範例的 `assert!` 寫在常數裡，需要 Rust 1.57 起支援的 const panic；1.37 推出時的匿名常數主要搭配巨集使用。

## 本版新增的棄用 API

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

| 模組 | 項目 | 官方說明 |
| --- | --- | --- |
| `std::os::unix::process` | [`CommandExt::before_exec`](https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#method.before_exec) | should be unsafe, use pre_exec instead |

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.37.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.37 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1370-2019-08-15)
- [Rust 1.37 發布公告](https://blog.rust-lang.org/releases/1.37.0/)
- [rust-lang/rust 原始碼 1.37.0](https://github.com/rust-lang/rust/tree/1.37.0)
