# Rust 1.15

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

## 版本介紹

Rust 1.15 最受期待的改變是自訂 `#[derive]`（custom derive，當時稱為「macros 1.1」）正式穩定：Serde、Diesel 這類函式庫可以讓你在結構上寫 `#[derive(Serialize)]` 就自動產生程式碼，不必再為了它們使用 nightly。同一版把 Rust 自己的建置系統改用 Rust 與 Cargo 撰寫，並重寫了切片排序演算法。升級時要注意：建置腳本（build script）不能再用 `env!("OUT_DIR")` 在編譯期讀取 `OUT_DIR`，要改成執行期用 `std::env` 讀取。

### 語言變更

- 程序式巨集（procedural macro）可以實作自訂 `#[derive]`，詳見 [程序式巨集](/macros-procedural)。
- 元組結構（tuple struct）可以沒有欄位；單一欄位或沒有欄位的元組結構可以用大括號建立。
- `?Sized` 可以寫在 `where` 子句裡，和寫在泛型參數旁邊等價，詳見 [特徵約束](/traits-bounds)。

### 標準庫重點

- 新增 `Iterator::min_by`、`Iterator::max_by`，用自訂比較函式找最小與最大值，詳見 [std::iter](/pkg-iter)。
- 新增 `Rc::strong_count`、`Rc::weak_count` 與 `Arc::strong_count`、`Arc::weak_count`，詳見 [std::rc](/pkg-rc)。
- 新增 `Receiver::try_iter`，一次取出通道中已到達的訊息而不阻塞，詳見 [std::sync::mpsc](/pkg-sync-mpsc)。
- 新增原子型別的 `get_mut`、`into_inner`，詳見 [std::sync::atomic](/pkg-sync-atomic)。
- 切片排序改寫為受 Timsort 啟發的混合合併排序，速度大幅提升，詳見 [切片](/pkg-slice)。
- `chars().count()`、`chars().last()` 變快；`fmt::Debug` 能正確顯示中文字元。

### Cargo 與工具

- `cargo test` 新增 `--all`，一次測試工作區（workspace）裡的所有套件，詳見 [工作區](/modules-workspaces)。
- 測試執行檔支援 `--list` 列出所有測試，以及 `--exact` 讓篩選條件完全比對名稱，詳見 [單元測試](/testing-unit)。
- 套件根目錄有 `build.rs` 卻沒在 `Cargo.toml` 設定 `build` 時會發出警告，為日後自動視為建置腳本預作準備。
- Rust 編譯器本身的建置系統改用 Rust 撰寫，以 Cargo 建置。

### 相容性注意

- 建置腳本不再能在編譯期透過 `env!("OUT_DIR")` 取得 `OUT_DIR`，已知會讓部分套件編譯失敗。
- 名稱解析（name resolution）依 RFC 1560 做了數項小調整，自 1.14 起已用 `legacy_imports` lint 預告。
- 只出現在關聯型別（associated type）裡的高階生命週期（higher-ranked lifetime）預設變成錯誤。
- `Peekable` 一旦 peek 到 `None`，之後會直接回傳該 `None`，不再重新詢問底層迭代器。

### Iterator::min_by 與 max_by

`min`、`max` 要求元素實作 `Ord`，只能用元素「本身」的大小比較。`min_by`、`max_by` 讓你傳入一個比較函式，自己決定什麼叫比較小，例如依字串長度比，或比較沒有實作 `Ord` 的浮點數。

範例中比較浮點數用的 `f64::total_cmp` 是 Rust 1.62 起才有的 API；1.15 當時要用 `partial_cmp` 再 `unwrap`。

範例：

```rust
fn main() {
    let words = ["apple", "fig", "banana", "kiwi"];

    let shortest = words.iter().min_by(|a, b| a.len().cmp(&b.len()));
    let longest = words.iter().max_by(|a, b| a.len().cmp(&b.len()));
    println!("最短：{:?}", shortest);
    println!("最長：{:?}", longest);

    let temps = [21.5_f64, 18.0, 25.25, 19.75];
    let max = temps.iter().copied().max_by(|a, b| a.total_cmp(b));
    println!("最高溫：{:?}", max);

    let empty: [i32; 0] = [];
    println!("空陣列：{:?}", empty.iter().min_by(|a, b| a.cmp(b)));
}
```

輸出：

```text
最短：Some("fig")
最長：Some("banana")
最高溫：Some(25.25)
空陣列：None
```

注意：迭代器是空的時候回傳 `None`；有多個元素一樣小時，`min_by` 回傳第一個，`max_by` 回傳最後一個。

### Rc::strong_count 與 weak_count

`Rc` 用參考計數（reference counting）讓多個擁有者共用同一份資料。`strong_count` 回傳目前有幾個 `Rc` 指向這份資料，`weak_count` 回傳有幾個 `Weak`；強參考歸零時資料就會被釋放，之後 `Weak::upgrade` 只會得到 `None`。這兩個函式適合用來觀察與除錯共享狀態，詳見 [智慧指標](/smart-pointers-overview)。

範例：

```rust
use std::rc::Rc;

fn main() {
    let config = Rc::new(String::from("debug=true"));
    println!("建立後 strong = {}", Rc::strong_count(&config));

    let a = Rc::clone(&config);
    let b = Rc::clone(&config);
    println!("複製兩次 strong = {}", Rc::strong_count(&config));

    let w = Rc::downgrade(&config);
    println!("weak = {}", Rc::weak_count(&config));

    drop(a);
    drop(b);
    println!("丟掉兩個 strong = {}", Rc::strong_count(&config));

    drop(config);
    println!("全部丟掉後 upgrade：{:?}", w.upgrade());
}
```

輸出：

```text
建立後 strong = 1
複製兩次 strong = 3
weak = 1
丟掉兩個 strong = 1
全部丟掉後 upgrade：None
```

注意：這兩個是關聯函式，要寫成 `Rc::strong_count(&x)`，不能寫 `x.strong_count()`，避免和內部型別的同名方法衝突。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.15.1` | 2017-02-09 | 否 | Fix IntoIter::as_mut_slice's signature；Compile compiler builtins with `-fPIC` on 32-bit platforms |

## 已知漏洞

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

### 仍影響 `1.15.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-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-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) |
| [CVE-2017-20004](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-20004) | std | MutexGuard\<Cell\<i32\>\> must not be Sync | 早於 1.19.0 | [1.19.0](/rust-1-19) |

本版已停止支援，不會再修正這些漏洞；請升級到 `1.100.1`。

## 資料來源

- [Rust 1.15 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1150-2017-02-02)
- [Rust 1.15 發布公告](https://blog.rust-lang.org/releases/1.15.0/)
- [rust-lang/rust 原始碼 1.15.0](https://github.com/rust-lang/rust/tree/1.15.0)
