# Rust 1.30

Rust 1.30 發布於 2018-10-25，目前最新修補版為 `1.30.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.30 讓程序式巨集（procedural macro）正式穩定，`#[derive]` 以外的屬性巨集與函式式巨集都能在 stable 上撰寫。同一版也為即將到來的 2018 edition 鋪路：路徑可以用 `crate::` 開頭、外部 crate 不必再寫 `::` 前綴、巨集可以用 `use` 匯入，還新增了原始識別字 `r#`。對新手來說，這版之後「用 `use` 匯入巨集」與「`crate::` 表示自己這個 crate」就是日常寫法。

### 語言變更

- 程序式巨集正式可用，屬性巨集與函式式巨集都能自訂，詳見 [程序式巨集](/macros-procedural)。
- 原始識別字（raw identifier）`r#` 讓關鍵字也能當作名稱，例如 `let r#for = true;`。
- 路徑可以用 `crate` 開頭指向 crate 根，`use crate::foo;` 就是 `src/lib.rs` 裡的 `foo`，詳見 [模組系統](/modules-system)。
- 使用外部 crate 不再需要 `::` 前綴，`::serde_json::from_str` 可以直接寫 `serde_json::from_str`。
- 其他 crate 匯出的巨集可以用 `use` 匯入與重新匯出；`#[macro_export]` 的巨集會放在 crate 根模組，詳見 [宣告式巨集](/macros-declarative)。
- 巨集新增 `vis` 片段，可以比對 `pub`、`pub(crate)` 這類可見性關鍵字。
- `#[used]` 讓 `static` 即使看似沒用到也不會被編譯器最佳化掉；`#[panic_handler]` 可以自訂 panic 的處理函式。
- trait 方法使用匿名參數改為棄用警告，在 2018 edition 會成為錯誤。

### 標準庫重點

- `Iterator::find_map` 找到第一個轉換成功的元素，詳見 [std::iter](/pkg-iter)。
- 新增 `str::trim_start`、`str::trim_end` 與 `_matches` 版本，用來取代 `trim_left` 系列，詳見 [str](/pkg-str)。
- `Ipv4Addr::LOCALHOST`、`Ipv4Addr::UNSPECIFIED`、`Ipv4Addr::BROADCAST`、`Ipv6Addr::LOCALHOST`、`Ipv6Addr::UNSPECIFIED` 常數穩定，詳見 [std::net](/pkg-net)。
- `ManuallyDrop` 的內部型別可以是大小不定（unsized）的型別。

### Cargo 與工具

- 在工作區裡執行 `cargo run` 不必再指定套件。
- 建置時會顯示進度條；`cargo doc` 支援 `--message-format=json`。
- `rustdoc` 新增 `--edition`、`--color`、`--error-format` 選項。
- `rustfmt`、`clippy` 等工具的屬性可以使用，例如 `#[rustfmt::skip]` 會略過下一個項目的格式化。

### 原始識別字 r#

Rust 的關鍵字不能直接當變數或函式名稱。加上 `r#` 前綴後，編譯器就把它當成一般的名稱。日常很少需要，但跨 edition 合作時特別有用：2018 edition 新增了 `async`、`dyn` 等關鍵字，舊 crate 用這些字命名的函式，新程式可以用 `r#async` 呼叫。

範例：

```rust
fn r#match(r#type: &str) -> bool {
    r#type == "fn"
}

fn main() {
    let r#for = "for";
    println!("{}", r#for);
    println!("{}", r#match("fn"));
    println!("{}", r#match("struct"));
}
```

輸出：

```text
for
true
false
```

### Iterator::find_map

`find_map` 等於「`filter_map` 之後取第一個」：對每個元素執行閉包，回傳第一個 `Some`，全部都是 `None` 就回傳 `None`。適合「從一串輸入中找出第一個能解析的值」。範例同時示範本版新增、用來取代 `trim_left` 的 `trim_start` 系列。

範例：

```rust
fn main() {
    let inputs = ["apple", "42", "banana", "7"];
    let first_number = inputs.iter().find_map(|s| s.parse::<i32>().ok());
    println!("{:?}", first_number);

    let none: Option<i32> = ["x", "y"].iter().find_map(|s| s.parse().ok());
    println!("{:?}", none);

    let padded = "  hello  ";
    println!("[{}]", padded.trim_start());
    println!("[{}]", padded.trim_end());
    println!("[{}]", "xxabcxx".trim_start_matches('x'));
}
```

輸出：

```text
Some(42)
None
[hello  ]
[  hello]
[abcxx]
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.30.1` | 2018-11-08 | 否 | Fixed overflow ICE in rustdoc；Cap Cargo progress bar width at 60 in MSYS terminals |

## 已知漏洞

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

### 仍影響 `1.30.1`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [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.30 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1300-2018-10-25)
- [Rust 1.30 發布公告](https://blog.rust-lang.org/releases/1.30.0/)
- [rust-lang/rust 原始碼 1.30.0](https://github.com/rust-lang/rust/tree/1.30.0)
