# Rust 1.36

Rust 1.36 發布於 2019-07-04，目前最新修補版為 `1.36.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.36 穩定了 `Future` trait，非同步程式的基礎型別終於進入標準庫，為 1.39 的 async/await 做好準備。同一版還穩定了 `alloc` crate、提供取代 `mem::uninitialized` 的 `MaybeUninit<T>`、把非詞法生命週期（NLL）帶到 2015 edition，並把 `HashMap` 換成速度更快、記憶體負擔更低的 hashbrown 實作。升級時要注意：2015 edition 的部分舊程式在 NLL 下會出現借用檢查警告，這些警告在之後的版本會升級為錯誤。

### 語言變更

- 非詞法生命週期（NLL）在 2015 edition 也啟用，詳見 [參考與借用](/ownership-references)。
- trait 物件中 trait 的順序不再影響語意，`dyn Send + fmt::Debug` 等同 `dyn fmt::Debug + Send`。

### 標準庫重點

- 穩定 `future::Future` 與 `task::Context`、`task::Poll`、`task::Waker`、`task::RawWaker`、`task::RawWakerVTable`，詳見 [std::future](/pkg-future) 與 [std::task](/pkg-task)。
- `alloc` crate 穩定，`#![no_std]` 的函式庫 crate 只要環境能配置堆積記憶體，就能使用 `Vec`、`Box`、`Arc`。
- `mem::MaybeUninit` 安全地表示「尚未初始化」的記憶體，詳見 [std::mem](/pkg-mem)。
- `HashMap` 改用 hashbrown 的實作，平均更快、記憶體負擔更低，詳見 [HashMap](/pkg-collections-hash-map)。
- `Iterator::copied` 把 `&T` 的迭代器轉成 `T`；`VecDeque::rotate_left`、`rotate_right` 旋轉佇列，詳見 [std::iter](/pkg-iter) 與 [集合](/pkg-collections)。
- 向量化 I/O 的 `io::IoSlice`、`io::IoSliceMut`、`Read::read_vectored`、`Write::write_vectored` 穩定，詳見 [std::io](/pkg-io)。
- `dbg!` 可以一次傳入多個參數，會回傳由各參數組成的元組。
- `String` 實作 `From<&String>` 與 `BorrowMut<str>`；`Result::is_ok`、`is_err` 加上 `#[must_use]`。

### Cargo 與工具

- `--offline` 讓 Cargo 完全不連網路，需要網路才能完成時直接回報錯誤。
- 把必要（非 optional）相依的名稱當作 feature 使用時，Cargo 會回報錯誤。

### 相容性注意

- 有了 `MaybeUninit` 之後，不再建議使用 `mem::uninitialized`，它在 1.39 棄用。

### Iterator::copied 與 VecDeque::rotate_left

對陣列呼叫 `iter()` 得到的是元素的參考 `&i32`，想收集成 `Vec<i32>` 時以前常寫 `.cloned()` 或 `.map(|x| *x)`。`copied` 專用於實作 `Copy` 的型別，意圖更明確。`VecDeque` 的 `rotate_left(n)` 把前 `n` 個元素移到尾端，`rotate_right(n)` 則相反。

範例：

```rust
use std::collections::VecDeque;

fn main() {
    let nums = [3, 1, 4];
    let owned: Vec<i32> = nums.iter().copied().collect();
    println!("{:?}", owned);

    let mut queue: VecDeque<i32> = (1..=5).collect();
    queue.rotate_left(2);
    println!("{:?}", queue);
    queue.rotate_right(1);
    println!("{:?}", queue);
}
```

輸出：

```text
[3, 1, 4]
[3, 4, 5, 1, 2]
[2, 3, 4, 5, 1]
```

### MaybeUninit

`MaybeUninit<T>` 是一塊「可能還沒放值」的記憶體。先用 `uninit()` 取得空間、用 `write` 寫入，確定已初始化後才呼叫 `unsafe` 的 `assume_init` 取出值。它取代了 `mem::uninitialized`：舊函式直接回傳一個「假裝已初始化」的值，對 `bool` 這類只允許特定位元樣式的型別會立刻造成未定義行為。

範例：

```rust
use std::mem::MaybeUninit;

fn main() {
    let mut slot = MaybeUninit::<u32>::uninit();
    slot.write(42);
    let value = unsafe { slot.assume_init() };
    println!("{}", value);
}
```

輸出：

```text
42
```

注意：在尚未寫入值時呼叫 `assume_init` 是未定義行為（undefined behavior）；一般程式很少需要 `MaybeUninit`，它主要用在效能敏感或與 C 互動的程式碼，詳見 [FFI](/lowlevel-ffi)。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.36.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.36 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1360-2019-07-04)
- [Rust 1.36 發布公告](https://blog.rust-lang.org/releases/1.36.0/)
- [rust-lang/rust 原始碼 1.36.0](https://github.com/rust-lang/rust/tree/1.36.0)
