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。語言變更
標準庫重點
- 以下函式可以在常數運算式中使用:原子型別的
new、Cell::new、RefCell::new、UnsafeCell::new、mem::size_of、mem::align_of、ptr::null、ptr::null_mut,以及整數的min_value、max_value,詳見 常數與靜態變數。 - 新增
RefCell::replace、RefCell::swap,詳見 std::cell。 - 新增
atomic::spin_loop_hint。 str::find搜尋char改用 memchr,多數情況快 10 倍;[u8]::contains也快了約 3 倍,詳見 str。SystemTime、Instant實作Hash;Rc/Arc可以從CString、OsString、PathBuf及其借用形式轉換而來,詳見 std::path。
Cargo 與工具
- 開發建置預設開啟增量編譯,可在
Cargo.toml或.cargo/config關閉,詳見 Cargo。 - release 建置預設 16 個 codegen unit。
- 工作區新增
workspace.default-members,覆寫虛擬工作區預設的--all,詳見 工作區。 - rustfmt 以
rustfmt-preview元件發布;之前用cargo install裝過的要先移除,詳見 開發工具。
相容性注意
- 浮點數的
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。
在 static 裡使用 const 建構函式
static 的初始值必須在編譯期就能算出來,所以以前不能寫 static COUNTER: AtomicUsize = AtomicUsize::new(0);。這版把這些建構函式標為可在常數運算式中呼叫,全域計數器和以型別大小決定的陣列長度都變得很直接。範例:
- 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());
- }
輸出:
- 處理了 4 個請求
- 緩衝區長度:8
注意:
size_of::<usize>() 在 64 位元平台是 8,32 位元平台是 4;Cell 與 RefCell 不是 Sync,不能直接放進 static,詳見 共享狀態。RefCell::replace 與 swap
RefCell 讓你在只有共享參考時也能修改內容。replace 放入新值並把舊值還給你;swap 交換兩個 RefCell 的內容。兩者都會在內部取得可變借用,比手動 borrow_mut() 再 mem::replace 簡潔。範例:
- 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());
- }
輸出:
- 舊值:草稿,新值:定稿
- left=[9] right=[1, 2]
注意:呼叫時如果該
RefCell 正被借用(例如還持有 borrow() 的結果),replace 與 swap 會 panic。本版新增的棄用 API
以下 API 從 Rust 1.24 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std::fmt | Formatter::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 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
| CVE-2024-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
| CVE-2023-38497 | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | 1.71.1 |
| CVE-2022-46176 | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | 1.66.1 |
| CVE-2022-36113 | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | 1.64.0 |
| CVE-2022-36114 | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | 1.64.0 |
| CVE-2022-21658 | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | 1.58.1 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-28878 | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| 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 |
| CVE-2021-28876 | std | Panic safety issue in Zip specialization | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| 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 |
| CVE-2021-28877 | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起,早於 1.51.0 | 1.51.0 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
| CVE-2019-16760 | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | 1.26.0 |
| CVE-2019-1010299 | std | vec_deque::Iter has unsound Debug implementation | 1.18.0 起,早於 1.30.0 | 1.30.0 |
| CVE-2018-1000622 | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | 1.27.1 |
| CVE-2018-25008 | std | Insufficient synchronization in Arc::get_mut | 1.3.0 起,早於 1.29.0 | 1.29.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。