# Rust 1.47

Rust 1.47 發布於 2020-10-08，目前最新修補版為 `1.47.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.47 沒有新的語言功能，最受期待的改變在標準庫：`Debug`、`PartialEq` 等特徵（trait）不再只實作到長度 32 的陣列，任何長度的陣列都能直接印出與比較。這是標準庫內部先用上 const generics 的成果，對新手來說，`[0; 34]` 印不出來這種奇怪的限制從此消失。同一版的 panic 回溯（backtrace）變得精簡，並升級到 LLVM 11。

### 語言變更

- 沒有使用的閉包（closure）會產生警告，詳見 [閉包](/closures-basics)。

### 標準庫重點

- `std`／`core` 中的特徵對任意長度的陣列都有實作，不再限於長度小於 33，詳見 [陣列](/pkg-array)。
- 新增 `Result::as_deref`、`Result::as_deref_mut`，詳見 [Result](/pkg-result)。
- 新增 `Range::is_empty`、`RangeInclusive::is_empty`，詳見 [運算子特徵](/pkg-ops)。
- 新增 `Vec::leak`、`pointer::offset_from`，以及常數 `f32::TAU`、`f64::TAU`（2π），詳見 [Vec](/pkg-vec) 與 [f64](/pkg-f64)。
- 整數的 `checked_*`、`saturating_*` 與 `char`／`u8` 的 `is_ascii_*` 等方法可以在 `const` 中使用。
- `ops::RangeFull` 與 `ops::Range` 實作 `Default`；`panic::Location` 實作 `Copy`、`Eq`、`Hash`、`Ord` 等特徵。

### Cargo 與工具

- `build-dependencies` 預設以 `opt-level` 0 編譯，可用 `[profile.release.build-override]` 覆寫，詳見 [Cargo](/modules-cargo)。
- `cargo help` 會顯示指令的 man page；`workspace.default-members` 會遵守 `workspace.exclude`。
- `RUST_BACKTRACE` 的輸出更精簡，只留下與錯誤相關的幾層；需要完整內容時設 `RUST_BACKTRACE=full`，詳見 [backtrace](/pkg-backtrace)。
- 新增 `-C control-flow-guard`，在 Windows 開啟 Control Flow Guard；rustdoc 新增 Ayu 主題。

### 相容性注意

- `linux-gnu` 目標至少需要核心 2.6.32 與 glibc 2.11。
- 最低支援的 Emscripten 版本提高到 1.39.20；CloudABI 目標降為 tier 3。
- `macro_rules!` 中程序式巨集的展開方式改變，不衛生（unhygienic）的巨集可能出現編譯錯誤。

### 任意長度陣列的特徵實作

陣列型別 `[T; N]` 把長度 `N` 寫在型別裡。以前標準庫只能為 0 到 32 每種長度各寫一份實作，長度 33 以上的陣列連 `{:?}` 都印不出來。現在 `Debug`、`PartialEq`、`Clone` 等特徵對任何長度都成立。

範例：

```rust
fn main() {
    let big = [7u8; 40];
    let copy = big;
    println!("{}", big == copy);
    println!("{:?}", &big[..5]);

    let zeros = [0u16; 34];
    let total: u16 = zeros.iter().sum();
    println!("{} {:?}", total, zeros == [0; 34]);
    println!("{:?}", [1; 33]);
}
```

輸出：

```text
true
[7, 7, 7, 7, 7]
0 true
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
```

### Result::as_deref 與 Range::is_empty

`Result<String, E>` 呼叫 `as_deref()` 會得到 `Result<&str, &E>`，不必移動原本的值就能用 `&str` 處理成功的結果。`is_empty()` 則回答「這個範圍有沒有任何元素」，例如 `3..3` 與 `1..=0` 都是空的。

範例：

```rust
fn find_user(id: u32) -> Result<String, String> {
    if id == 1 {
        Ok(String::from("alice"))
    } else {
        Err(format!("user {id} not found"))
    }
}

fn main() {
    let found = find_user(1);
    let name: Result<&str, &String> = found.as_deref();
    println!("{:?}", name);

    let missing = find_user(9);
    println!("{:?}", missing.as_deref());

    println!("{:?}", (3..3).is_empty());
    println!("{:?}", (1..=0).is_empty());
}
```

輸出：

```text
Ok("alice")
Err("user 9 not found")
true
true
```

### TAU 常數

`TAU` 等於 2π，也就是一整圈的弧度。計算圓周長或角度換算時，用 `TAU` 比 `2.0 * PI` 更直接。

範例：

```rust
use std::f64::consts::{PI, TAU};

fn main() {
    let r = 2.0;
    println!("{:.4}", TAU * r);
    println!("{}", TAU == 2.0 * PI);
}
```

輸出：

```text
12.5664
true
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.47.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.47 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1470-2020-10-08)
- [Rust 1.47 發布公告](https://blog.rust-lang.org/releases/1.47.0/)
- [rust-lang/rust 原始碼 1.47.0](https://github.com/rust-lang/rust/tree/1.47.0)
