# Rust 1.11

Rust 1.11 發布於 2016-08-18，目前最新修補版為 `1.11.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.11 的工作大多在編譯器內部，為 MIR 成為預設與增量編譯（incremental compilation）打基礎。使用者看得到的改變有：Cargo 支援 1.10 引入的 `cdylib` crate 類型、`Iterator` 新增 `sum` 與 `product`、`BTreeMap` 可以 `append` 與 `split_off`，`HashMap` 的預設雜湊從 SipHash 2-4 換成較快的 SipHash 1-3。升級要注意：`const` 與 `static` 不能是不定大小型別；`macro_rules!` 的 follow-set 限制（1.10 起為警告）正式生效。

### 語言變更

- 支援巢狀的 `cfg_attr` 屬性。
- 區塊結尾可以放產生陳述式的大括號巨集呼叫；巨集也能在特徵定義內展開，詳見 [宣告式巨集](/macros-declarative)。
- 由巨集展開出來的 `#[macro_use]` 能正確運作。

### 標準庫重點

- `Iterator::sum` 與 `Iterator::product` 穩定，詳見 [std::iter](/pkg-iter)。
- `BTreeMap`、`BTreeSet` 新增 `append` 與 `split_off`，`BinaryHeap` 新增 `append`，詳見 [BTreeMap](/pkg-collections-btree-map)。
- `Cell::get_mut` 與 `RefCell::get_mut` 在已有可變參考時直接取得內部值，詳見 [std::cell](/pkg-cell)。
- `assert_eq!` 和 `assert!` 一樣可以附上自訂錯誤訊息，詳見 [單元測試](/testing-unit)。
- 主執行緒的名稱改為 `"main"`，不再是 `"<main>"`，詳見 [std::thread](/pkg-thread)。
- `thread_local!` 可以一次定義多個並套用屬性；`Cow` 實作 `Default`；範圍型別實作 `Hash`。
- `f32`／`f64` 的 `to_degrees`、`to_radians` 也放進 libcore。
- 預設 `HashMap` 雜湊器改為 SipHash 1-3，速度更快，仍足以防範碰撞攻擊，詳見 [HashMap](/pkg-collections-hash-map)；`Ipv4Addr` 的比較快 10 倍。

### Cargo 與工具

- Cargo 支援 `cdylib`：在 `Cargo.toml` 寫 `crate-type = ["cdylib"]` 即可建置，詳見 [FFI](/lowlevel-ffi)。
- 建置腳本可以輸出警告，`-vv` 會顯示所有 crate 的警告。
- 檔案有未提交變更（dirty）時禁止發布 crate；不再對 `package.metadata` 鍵發出警告，留給其他工具擴充，詳見 [Cargo](/modules-cargo)。
- Windows 主控台支援顏色，stderr 的顏色也能設定。
- 更多錯誤有了錯誤代碼與延伸說明。

### 相容性注意

- `const` 與 `static` 不能是不定大小型別，詳見 [常數](/basics-constants)。
- `macro_rules!` 的 follow-set 規則正式啟用。
- `cfg` 屬性的處理經過重構，少數邊緣情況會受影響。
- 不能再指定遞移（transitive）相依的 features。

### Iterator::sum 與 product

`sum` 把所有元素加總，`product` 把所有元素相乘。回傳型別通常要寫在變數上（例如 `let total: i32`），編譯器才知道要加總成哪種型別。空的迭代器 `sum` 得到 0、`product` 得到 1。

範例：

```rust
fn main() {
    let prices = [120, 80, 45];
    let total: i32 = prices.iter().sum();
    println!("總價 {}", total);

    let factorial: u64 = (1..=10).product();
    println!("10! = {}", factorial);

    let empty: Vec<i32> = Vec::new();
    let s: i32 = empty.iter().sum();
    let p: i32 = empty.iter().product();
    println!("空集合：sum = {}, product = {}", s, p);
}
```

輸出：

```text
總價 245
10! = 3628800
空集合：sum = 0, product = 1
```

注意：Rust 1.13 起 `sum` 與 `product` 沿用一般算術的溢位檢查，結果溢位時在除錯建置會 panic。

### BTreeMap 的 split_off 與 append

`split_off(&key)` 把大於等於 `key` 的項目切出去成為新的映射，原映射只留下較小的部分；`append(&mut other)` 把另一個映射的內容全部搬進來，`other` 會被清空。

範例：

```rust
use std::collections::BTreeMap;

fn main() {
    let mut scores = BTreeMap::new();
    for (name, s) in [("amy", 72), ("ben", 91), ("cody", 58), ("dana", 85)] {
        scores.insert(name, s);
    }

    let second_half = scores.split_off("c");
    println!("前半 {:?}", scores);
    println!("後半 {:?}", second_half);

    let mut merged = second_half;
    merged.append(&mut scores);
    println!("合併 {:?}，原本剩 {}", merged, scores.len());
}
```

輸出：

```text
前半 {"amy": 72, "ben": 91}
後半 {"cody": 58, "dana": 85}
合併 {"amy": 72, "ben": 91, "cody": 58, "dana": 85}，原本剩 0
```

### 主執行緒名稱與 assert_eq! 自訂訊息

`thread::current().name()` 在主執行緒回傳 `Some("main")`，用 `thread::Builder` 建立的執行緒則是自訂的名稱，方便在日誌中辨識。`assert_eq!` 的第三個參數起是格式化的錯誤訊息，斷言失敗時一起印出。

範例：

```rust
use std::thread;

fn main() {
    println!("{:?}", thread::current().name());

    let child = thread::Builder::new()
        .name("worker-1".to_string())
        .spawn(|| thread::current().name().map(|s| s.to_string()))
        .unwrap();
    println!("{:?}", child.join().unwrap());

    let a = 1 + 1;
    assert_eq!(a, 2, "算術壞了：a = {}", a);
    println!("assert_eq! 通過");
}
```

輸出：

```text
Some("main")
Some("worker-1")
assert_eq! 通過
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.11.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-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.11 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1110-2016-08-18)
- [Rust 1.11 發布公告](https://blog.rust-lang.org/releases/1.11.0/)
- [rust-lang/rust 原始碼 1.11.0](https://github.com/rust-lang/rust/tree/1.11.0)
