# Rust 1.19

Rust 1.19 發布於 2017-07-20，目前最新修補版為 `1.19.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.19 帶來兩個長期期待的語言功能：與 C 相容的 `union`，以及 `loop` 可以用 `break 值` 回傳結果，讓 `loop` 也成為有值的運算式。標準庫加入 `eprint!`／`eprintln!`，直接輸出到標準錯誤；Windows 上 `rustc` 也終於能自動找到 Visual Studio 2017 的建置工具。升級時要注意：穩定版與 beta 版不再接受 `-Z` 不穩定旗標，舊的 `cargo-check` 外掛因此失效，請改用內建的 `cargo check`。

### 語言變更

- `loop` 可以用 `break 值` 回傳值，例如 `let x = loop { break 7; };`，詳見 [迴圈](/control-loops)。
- 新增 `union`，只能包含 `Copy` 型別且不能實作 `Drop`，讀取欄位需要 `unsafe`，詳見 [unsafe](/lowlevel-unsafe)。
- 沒有捕獲環境的閉包可以強制轉型為函式指標 `fn`，詳見 [閉包](/closures-basics)。
- 元組結構可以用數字欄位名建立，例如 `Point { 0: 7, 1: 0 }`。
- 巨集遞迴上限由 64 提高到 1024，並新增偵測未使用巨集的 lint，詳見 [宣告式巨集](/macros-declarative)。

### 標準庫重點

- `eprint!`、`eprintln!` 加入 prelude，用法與 `print!` 相同但寫到標準錯誤，詳見 [prelude](/pkg-prelude)。
- 新增 `cmp::Reverse`，把排序方向反過來，詳見 [std::cmp](/pkg-cmp)。
- 新增 `thread::ThreadId`，詳見 [std::thread](/pkg-thread)。
- 新增 `Command::envs`，一次設定多個環境變數，詳見 [std::process](/pkg-process)。
- 新增 `OsString::shrink_to_fit`；`String` 實作 `FromIterator<Cow<str>>` 與 `Extend<Cow<str>>`；`Vec` 實作 `From<&mut [T]>`。

### Cargo 與工具

- 建置腳本可以用 `println!("cargo:rustc-env=FOO=bar");` 為編譯中的 crate 設定環境變數。
- 工作區成員可以用 glob 樣式指定；`--all` 可搭配 `--exclude` 排除特定套件；`cargo bench` 支援 `--all`，詳見 [工作區](/modules-workspaces)。
- `--features` 接受以逗號或空白分隔的多個值；收到 crates.io 的 5xx 錯誤會自動重試。
- Cargo 不再為 crates.io 索引建立本機工作目錄，註冊表占用空間更小、複製更快。
- `rustc` 支援 Visual Studio 2017；改用 LLVM 4.0.1；發布 xz 壓縮的安裝包。

### 相容性注意

- 穩定版禁止使用 `-Z` 旗標。
- `MutexGuard<T>` 只有在 `T: Sync` 時才是 `Sync`，詳見 [Send 與 Sync](/concurrency-send-sync)。
- 以下由警告改為硬錯誤：在 match 模式中使用浮點數常數、使用沒有 derive `PartialEq` 與 `Eq` 的結構或列舉常數、命名為 `'_` 的生命週期、在模組外使用私有的 `extern crate`。
- `use ::self::foo;` 與 `42._` 這類浮點字面值成為錯誤；重新匯出私有列舉變體也是錯誤。

### loop 回傳值與閉包轉函式指標

以前 `loop` 只是敘述，想從迴圈帶出結果要先宣告變數、在迴圈裡賦值再 `break`。現在 `break` 後面可以接一個值，整個 `loop` 就等於這個值。同一版也允許沒有捕獲任何變數的閉包當作 `fn` 函式指標傳遞。

範例：

```rust
fn apply(f: fn(i32) -> i32, x: i32) -> i32 {
    f(x)
}

fn main() {
    let data = [3, 8, 15, 4, 22];
    let mut i = 0;
    let first_big = loop {
        if i == data.len() {
            break None;
        }
        if data[i] > 10 {
            break Some((i, data[i]));
        }
        i += 1;
    };
    println!("第一個大於 10 的：{:?}", first_big);

    let double: fn(i32) -> i32 = |x| x * 2;
    println!("{}", apply(double, 21));
    println!("{}", apply(|x| x - 1, 21));
}
```

輸出：

```text
第一個大於 10 的：Some((2, 15))
42
20
```

注意：只有 `loop` 可以 `break` 帶值，`while` 與 `for` 不行；有捕獲外部變數的閉包不能轉成 `fn`，要改用泛型或 `impl Fn`。

### union

`union` 像沒有標籤（tag）的列舉：所有欄位共用同一塊記憶體，程式不會記錄目前存的是哪一個欄位。主要用途是對接 C 函式庫的 union，或做節省空間的底層資料結構。因為編譯器無法確認你讀的欄位是否就是最後寫入的那個，讀取欄位必須放在 `unsafe` 區塊。

範例：

```rust
#[repr(C)]
union IntOrFloat {
    i: u32,
    f: f32,
}

fn main() {
    let mut v = IntOrFloat { f: 1.0 };
    let bits = unsafe { v.i };
    println!("1.0 的位元：{:#010x}", bits);

    v.i = 0x4049_0fdb;
    let f = unsafe { v.f };
    println!("0x40490fdb 當成 f32：{}", f);

    println!("大小：{} bytes", std::mem::size_of::<IntOrFloat>());
}
```

輸出：

```text
1.0 的位元：0x3f800000
0x40490fdb 當成 f32：3.1415927
大小：4 bytes
```

注意：讀取一個沒有被寫入有效值的欄位可能是未定義行為；單純想看浮點數的位元，用安全的 `f32::to_bits` 與 `f32::from_bits` 即可，詳見 [f64 與浮點數](/pkg-f64)。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.19.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-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-1000657](https://github.com/rust-lang/rust/issues/44800) | std | Buffer overflow vulnerability in VecDeque::reserve() | 1.3.0 起，早於 1.22.0 | [1.22.0](/rust-1-22) |
| [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.19 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1190-2017-07-20)
- [Rust 1.19 發布公告](https://blog.rust-lang.org/releases/1.19.0/)
- [rust-lang/rust 原始碼 1.19.0](https://github.com/rust-lang/rust/tree/1.19.0)
