# Rust 1.8

Rust 1.8 發布於 2016-04-14，目前最新修補版為 `1.8.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.8 讓 `+=`、`-=` 這類複合指定運算子可以透過 `AddAssign`、`SubAssign` 等特徵多載，自訂型別也能寫 `a += b`；沒有欄位的結構也能用大括號 `struct Foo {}` 宣告。標準庫穩定約 20 個 API，包括表示時間點的 `time::Instant` 與 `time::SystemTime`、`str::encode_utf16`。升級要注意：32 位元 MSVC 建置支援 unwinding，`i686-pc-windows-msvc` 升為 tier 1 平台；新的不穩定編譯器旗標不再能在穩定版使用。

### 語言變更

- 複合指定運算可透過 `AddAssign`、`SubAssign`、`MulAssign`、`DivAssign`、`RemAssign`、`BitAndAssign`、`BitOrAssign`、`BitXorAssign`、`ShlAssign`、`ShrAssign` 多載，詳見 [std::ops](/pkg-ops)。
- 空結構可以寫成 `struct Foo { }`，與 `struct Foo;` 並存，詳見 [結構](/structs-basics)。

### 標準庫重點

- `time::Instant`（單調遞增的時間點，量測經過時間用）與 `time::SystemTime`（系統時鐘）穩定，附 `now`、`elapsed`、`duration_since` 與 `UNIX_EPOCH`，詳見 [std::time](/pkg-time)。
- `str::encode_utf16`（原名 `utf16_units`）把字串編成 UTF-16 單元，詳見 [&str](/pkg-str)。
- `Ref::map`、`RefMut::map` 讓 `RefCell` 的借用只指向內部某一部分，詳見 [std::cell](/pkg-cell)。
- `ptr::drop_in_place` 穩定，詳見 [std::ptr](/pkg-ptr)。
- `write!`／`writeln!` 在引數無法格式化時會正確回報錯誤，詳見 [std::fmt](/pkg-fmt)。
- `sync::mpsc` 的所有型別實作 `Debug`，詳見 [std::sync::mpsc](/pkg-sync-mpsc)。

### Cargo 與工具

- 新增 `cargo init`，在目前目錄建立 Cargo 專案，不像 `cargo new` 會新建子目錄，詳見 [Cargo](/modules-cargo)。
- 新增 `cargo metadata` 子命令，取得專案的 metadata。
- `.cargo/config` 的 `[term]` 區段可設定 `verbose` 與 `color`；字串或整數設定能用環境變數覆寫，例如 `CARGO_BUILD_JOBS`。
- 平台專屬相依可用 Rust 的 `cfg` 語法描述目標平台。
- `rustc --print cfg` 印出目前目標平台定義的 `cfg`；非窮舉 `match` 的錯誤最多列出 3 個缺少的變體並告知總數。
- 新的、以 Cargo 為基礎並用 Rust 撰寫的編譯器建置系統開始可用，預計取代 Makefile。

### 相容性注意

- Unix 的 `MetadataExt` 不再回傳平台專屬型別，改回傳加寬的整數。
- 不能再用 `Variant(..)` 比對沒有欄位的列舉變體（1.6 起為警告），詳見 [match](/control-match)。
- 從檔案載入的模組只能出現在其他模組內，不能放在任意區塊中，詳見 [模組系統](/modules-system)。
- Unix 上堆疊溢位改為執行期中止（abort），不再是 SIGSEGV。
- `Command::spawn` 等函式在命令列引數含 `NUL` 字元時回傳錯誤，詳見 [std::process](/pkg-process)。

### 多載 += 運算子

為型別實作 `AddAssign`，就能對它使用 `+=`；右邊的型別可以和左邊不同，例如下面的 `MulAssign<i64>` 讓金額乘上一個整數。方法參數是 `&mut self`，直接修改左邊的值，不會產生新值。

範例：

```rust
use std::ops::{AddAssign, MulAssign};

#[derive(Debug, Clone, Copy)]
struct Money {
    cents: i64,
}

impl AddAssign for Money {
    fn add_assign(&mut self, other: Money) {
        self.cents += other.cents;
    }
}

impl MulAssign<i64> for Money {
    fn mul_assign(&mut self, factor: i64) {
        self.cents *= factor;
    }
}

fn main() {
    let mut wallet = Money { cents: 1_000 };
    wallet += Money { cents: 250 };
    println!("{:?}", wallet);
    wallet *= 3;
    println!("{:?}", wallet);
}
```

輸出：

```text
Money { cents: 1250 }
Money { cents: 3750 }
```

### 大括號空結構與 encode_utf16

`struct Marker {}` 與 `struct Unit;` 都是沒有欄位的結構，建立時分別寫 `Marker {}` 與 `Unit`；開發中常在「有欄位」與「沒欄位」之間切換時，大括號寫法改起來比較順。`encode_utf16` 把字串轉成 UTF-16 編碼單元，和 Windows API 或 JavaScript 交換字串時會用到。

範例：

```rust
#[derive(Debug, Default)]
struct Marker {}

#[derive(Debug)]
struct Unit;

fn main() {
    let a = Marker {};
    let b = Marker::default();
    let c = Unit;
    println!("{:?} {:?} {:?}", a, b, c);

    let units: Vec<u16> = "Rust🦀".encode_utf16().collect();
    println!("{} 個 UTF-16 單元：{:x?}", units.len(), units);
}
```

輸出：

```text
Marker Marker Unit
6 個 UTF-16 單元：[52, 75, 73, 74, d83e, dd80]
```

注意：BMP 以外的字元（例如 emoji）在 UTF-16 中佔兩個單元（代理對，surrogate pair），所以上例 5 個字元得到 6 個單元。

## 本版新增的棄用 API

以下 API 從 Rust 1.8 起標記為棄用（deprecated），新程式不要再使用；說明欄為官方原文。

| 模組 | 項目 | 官方說明 |
| --- | --- | --- |
| `std::os::darwin::fs` | [`MetadataExt::as_raw_stat`](https://doc.rust-lang.org/std/os/darwin/fs/trait.MetadataExt.html#tymethod.as_raw_stat) | deprecated in favor of the accessor methods of this trait |
| `std::os::linux::fs` | [`MetadataExt::as_raw_stat`](https://doc.rust-lang.org/std/os/linux/fs/trait.MetadataExt.html#tymethod.as_raw_stat) | other methods of this trait are now preferred |
| `std::os::linux::raw` | [`blkcnt_t`](https://doc.rust-lang.org/std/os/linux/raw/type.blkcnt_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`blksize_t`](https://doc.rust-lang.org/std/os/linux/raw/type.blksize_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`dev_t`](https://doc.rust-lang.org/std/os/linux/raw/type.dev_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`ino_t`](https://doc.rust-lang.org/std/os/linux/raw/type.ino_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`mode_t`](https://doc.rust-lang.org/std/os/linux/raw/type.mode_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`nlink_t`](https://doc.rust-lang.org/std/os/linux/raw/type.nlink_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`off_t`](https://doc.rust-lang.org/std/os/linux/raw/type.off_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`pthread_t`](https://doc.rust-lang.org/std/os/linux/raw/type.pthread_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`stat`](https://doc.rust-lang.org/std/os/linux/raw/struct.stat.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::linux::raw` | [`time_t`](https://doc.rust-lang.org/std/os/linux/raw/type.time_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`blkcnt_t`](https://doc.rust-lang.org/std/os/unix/raw/type.blkcnt_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`blksize_t`](https://doc.rust-lang.org/std/os/unix/raw/type.blksize_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`dev_t`](https://doc.rust-lang.org/std/os/unix/raw/type.dev_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`gid_t`](https://doc.rust-lang.org/std/os/unix/raw/type.gid_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`ino_t`](https://doc.rust-lang.org/std/os/unix/raw/type.ino_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`mode_t`](https://doc.rust-lang.org/std/os/unix/raw/type.mode_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`nlink_t`](https://doc.rust-lang.org/std/os/unix/raw/type.nlink_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`off_t`](https://doc.rust-lang.org/std/os/unix/raw/type.off_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`pid_t`](https://doc.rust-lang.org/std/os/unix/raw/type.pid_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`pthread_t`](https://doc.rust-lang.org/std/os/unix/raw/type.pthread_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`time_t`](https://doc.rust-lang.org/std/os/unix/raw/type.time_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
| `std::os::unix::raw` | [`uid_t`](https://doc.rust-lang.org/std/os/unix/raw/type.uid_t.html) | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.8.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-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.8 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-180-2016-04-14)
- [Rust 1.8 發布公告](https://blog.rust-lang.org/releases/1.8.0/)
- [rust-lang/rust 原始碼 1.8.0](https://github.com/rust-lang/rust/tree/1.8.0)
