# Rust 1.35

Rust 1.35 發布於 2019-05-23，目前最新修補版為 `1.35.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.35 讓 `Box<dyn FnOnce>`、`Box<dyn FnMut>`、`Box<dyn Fn>` 直接實作對應的閉包 trait，裝箱的閉包可以像一般函式一樣呼叫或傳遞。範圍型別新增 `contains`，`dbg!()` 也能不帶參數使用，只印出所在的檔案與行號。這版沒有破壞性變更，Rust 工具鏈也開始原生支援以 musl 為基礎的 Linux 發行版。

### 語言變更

- `FnOnce`、`FnMut`、`Fn` 分別為 `Box<dyn FnOnce>`、`Box<dyn FnMut>`、`Box<dyn Fn>` 實作，詳見 [閉包](/closures-basics) 與 [Box](/pkg-boxed)。
- 閉包可以強制轉型為 `unsafe fn` 函式指標，詳見 [unsafe](/lowlevel-unsafe)。

### 標準庫重點

- `Range`、`RangeFrom`、`RangeTo`、`RangeInclusive`、`RangeToInclusive` 新增 `contains`，詳見 [std::ops](/pkg-ops)。
- `f32::copysign`、`f64::copysign` 把一個數的正負號套到另一個數上，詳見 [f64](/pkg-f64)。
- `Option::copied` 把 `Option<&T>` 轉成 `Option<T>`（`T` 需實作 `Copy`），詳見 [Option](/pkg-option)。
- `RefCell::replace_with` 與 `RefCell::map_split` 穩定，詳見 [std::cell](/pkg-cell)；新增 `ptr::hash`。
- `dbg!()` 不帶參數時只印出檔案與行號，方便追蹤程式走了哪個分支。
- `Thread` 的 `Debug` 輸出包含執行緒 ID；`{:#?}` 展開格式的最後一個欄位後面會加逗號。
- 所有 `NonZero` 數值型別實作 `FromStr`；`str::make_ascii_lowercase` 等就地 ASCII 大小寫轉換最快提升 4 倍。

### Cargo 與工具

- 建置腳本可以設定 `cargo:rustc-cdylib-link-arg`，在建置 `cdylib` 時傳遞自訂連結器參數。
- 新增 `wasm32-unknown-wasi` 目標。

### Range::contains

判斷數值是否落在某個區間，以前要寫 `x >= 18 && x < 65`，現在可以直接問範圍 `(18..65).contains(&x)`。半開區間 `a..b` 不含 `b`，閉區間 `a..=b` 含 `b`，只有上界的 `..b` 也能用。參數是參考，所以要寫 `&age`。

範例：

```rust
fn main() {
    let ages = 18..65;
    for age in [10, 18, 64, 65] {
        println!("{} {}", age, ages.contains(&age));
    }
    println!("{}", (1..=10).contains(&10));
    println!("{}", (..0).contains(&-3));
}
```

輸出：

```text
10 false
18 true
64 true
65 false
true
true
```

### 直接呼叫 Box<dyn FnOnce>

想把「不同的閉包」放進同一個 `Vec`，就要用 trait 物件 `Box<dyn FnOnce()>`。以前呼叫裝箱的 `FnOnce` 需要繞路，現在直接 `task()` 就能執行；`Box<dyn Fn>` 也能直接傳給 `map` 這類要求閉包的方法。

範例：

```rust
fn main() {
    let name = String::from("Ferris");
    let tasks: Vec<Box<dyn FnOnce() -> String>> = vec![
        Box::new(move || format!("hello {}", name)),
        Box::new(|| String::from("bye")),
    ];
    for task in tasks {
        println!("{}", task());
    }

    let double: Box<dyn Fn(i32) -> i32> = Box::new(|x| x * 2);
    let result: Vec<i32> = [1, 2, 3].iter().copied().map(double).collect();
    println!("{:?}", result);
}
```

輸出：

```text
hello Ferris
bye
[2, 4, 6]
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.35.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.35 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1350-2019-05-23)
- [Rust 1.35 發布公告](https://blog.rust-lang.org/releases/1.35.0/)
- [rust-lang/rust 原始碼 1.35.0](https://github.com/rust-lang/rust/tree/1.35.0)
