# Rust 1.24

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

## 版本介紹

Rust 1.24 帶來兩個讓開發更舒服的工具改進：可以用 `rustup component add rustfmt-preview` 安裝程式碼格式化工具 rustfmt 的預覽版，以及開發建置預設開啟增量編譯（incremental compilation），只重新編譯改過的部分。標準庫方面，`AtomicUsize::new`、`Cell::new`、`mem::size_of` 等函式可以用在常數運算式中，直接拿來初始化 `static`。升級時要注意：release 建置預設改為 16 個 codegen unit，編譯更快但執行檔可能稍慢，追求極致效能要在 `Cargo.toml` 設定 `codegen-units = 1`。

### 語言變更

- 新增 `extern "sysv64"` 呼叫慣例，詳見 [FFI](/lowlevel-ffi)。
- 存取 `#[repr(packed)]` 結構的欄位需要 `unsafe`，詳見 [unsafe](/lowlevel-unsafe)。

### 標準庫重點

- 以下函式可以在常數運算式中使用：原子型別的 `new`、`Cell::new`、`RefCell::new`、`UnsafeCell::new`、`mem::size_of`、`mem::align_of`、`ptr::null`、`ptr::null_mut`，以及整數的 `min_value`、`max_value`，詳見 [常數與靜態變數](/basics-constants)。
- 新增 `RefCell::replace`、`RefCell::swap`，詳見 [std::cell](/pkg-cell)。
- 新增 `atomic::spin_loop_hint`。
- `str::find` 搜尋 `char` 改用 memchr，多數情況快 10 倍；`[u8]::contains` 也快了約 3 倍，詳見 [str](/pkg-str)。
- `SystemTime`、`Instant` 實作 `Hash`；`Rc`／`Arc` 可以從 `CString`、`OsString`、`PathBuf` 及其借用形式轉換而來，詳見 [std::path](/pkg-path)。

### Cargo 與工具

- 開發建置預設開啟增量編譯，可在 `Cargo.toml` 或 `.cargo/config` 關閉，詳見 [Cargo](/start-cargo)。
- release 建置預設 16 個 codegen unit。
- 工作區新增 `workspace.default-members`，覆寫虛擬工作區預設的 `--all`，詳見 [工作區](/modules-workspaces)。
- rustfmt 以 `rustfmt-preview` 元件發布；之前用 `cargo install` 裝過的要先移除，詳見 [開發工具](/modules-tools)。

### 相容性注意

- 浮點數的 `Debug` 輸出一律帶小數點，例如 `1.0` 不再印成 `1`。
- `Ipv6Addr` 拒絕多餘的 `::`；`column!()` 改為從 1 起算。
- `Formatter::flags` 已棄用；`fmt::Arguments` 不能再跨執行緒共用；元組結構成員的前導零成為錯誤。
- Cargo 呼叫編譯器時使用不同的工作目錄。
- `spin_loop_hint` 與整數的 `min_value`、`max_value` 後來都已棄用，新程式碼改用 `hint::spin_loop` 與 `MIN`／`MAX` 常數，詳見 [棄用 API](/deprecations)。

### 在 static 裡使用 const 建構函式

`static` 的初始值必須在編譯期就能算出來，所以以前不能寫 `static COUNTER: AtomicUsize = AtomicUsize::new(0);`。這版把這些建構函式標為可在常數運算式中呼叫，全域計數器和以型別大小決定的陣列長度都變得很直接。

範例：

```rust
use std::mem::size_of;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

static REQUESTS: AtomicUsize = AtomicUsize::new(0);
const WORD: usize = size_of::<usize>();

fn handle() {
    REQUESTS.fetch_add(1, Ordering::SeqCst);
}

fn main() {
    let workers: Vec<_> = (0..4).map(|_| thread::spawn(handle)).collect();
    for w in workers {
        w.join().unwrap();
    }
    println!("處理了 {} 個請求", REQUESTS.load(Ordering::SeqCst));

    let buffer = [0u8; WORD];
    println!("緩衝區長度：{}", buffer.len());
}
```

輸出：

```text
處理了 4 個請求
緩衝區長度：8
```

注意：`size_of::<usize>()` 在 64 位元平台是 8，32 位元平台是 4；`Cell` 與 `RefCell` 不是 `Sync`，不能直接放進 `static`，詳見 [共享狀態](/concurrency-shared-state)。

### RefCell::replace 與 swap

`RefCell` 讓你在只有共享參考時也能修改內容。`replace` 放入新值並把舊值還給你；`swap` 交換兩個 `RefCell` 的內容。兩者都會在內部取得可變借用，比手動 `borrow_mut()` 再 `mem::replace` 簡潔。

範例：

```rust
use std::cell::RefCell;

fn main() {
    let current = RefCell::new(String::from("草稿"));
    let old = current.replace(String::from("定稿"));
    println!("舊值：{}，新值：{}", old, current.borrow());

    let left = RefCell::new(vec![1, 2]);
    let right = RefCell::new(vec![9]);
    left.swap(&right);
    println!("left={:?} right={:?}", left.borrow(), right.borrow());
}
```

輸出：

```text
舊值：草稿，新值：定稿
left=[9] right=[1, 2]
```

注意：呼叫時如果該 `RefCell` 正被借用（例如還持有 `borrow()` 的結果），`replace` 與 `swap` 會 panic。

## 本版新增的棄用 API

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

| 模組 | 項目 | 官方說明 |
| --- | --- | --- |
| `std::fmt` | [`Formatter::flags`](https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.flags) | use the sign_plus, sign_minus, alternate, or sign_aware_zero_pad methods instead |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.24.1` | 2018-03-01 | 否 | Do not abort when unwinding through FFI；Emit UTF-16 files for linker arguments on Windows；Make the error index generator work again；Cargo will warn on Windows 7 if an update is needed. |

## 已知漏洞

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

### 仍影響 `1.24.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-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-2019-1010299](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-1010299) | std | vec_deque::Iter has unsound Debug implementation | 1.18.0 起，早於 1.30.0 | [1.30.0](/rust-1-30) |
| [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) |

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

## 資料來源

- [Rust 1.24 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1240-2018-02-15)
- [Rust 1.24 發布公告](https://blog.rust-lang.org/releases/1.24.0/)
- [rust-lang/rust 原始碼 1.24.0](https://github.com/rust-lang/rust/tree/1.24.0)
