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。
  • 存取 #[repr(packed)] 結構的欄位需要 unsafe,詳見 unsafe。

標準庫重點


  • 以下函式可以在常數運算式中使用:原子型別的 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);。這版把這些建構函式標為可在常數運算式中呼叫,全域計數器和以型別大小決定的陣列長度都變得很直接。
範例:
  1. use std::mem::size_of;
  2. use std::sync::atomic::{AtomicUsize, Ordering};
  3. use std::thread;
  4. static REQUESTS: AtomicUsize = AtomicUsize::new(0);
  5. const WORD: usize = size_of::<usize>();
  6. fn handle() {
  7. REQUESTS.fetch_add(1, Ordering::SeqCst);
  8. }
  9. fn main() {
  10. let workers: Vec<_> = (0..4).map(|_| thread::spawn(handle)).collect();
  11. for w in workers {
  12. w.join().unwrap();
  13. }
  14. println!("處理了 {} 個請求", REQUESTS.load(Ordering::SeqCst));
  15. let buffer = [0u8; WORD];
  16. println!("緩衝區長度:{}", buffer.len());
  17. }

輸出:
  1. 處理了 4 個請求
  2. 緩衝區長度:8

注意:size_of::<usize>() 在 64 位元平台是 8,32 位元平台是 4;Cell 與 RefCell 不是 Sync,不能直接放進 static,詳見 共享狀態。

RefCell::replace 與 swap


RefCell 讓你在只有共享參考時也能修改內容。replace 放入新值並把舊值還給你;swap 交換兩個 RefCell 的內容。兩者都會在內部取得可變借用,比手動 borrow_mut() 再 mem::replace 簡潔。
範例:
  1. use std::cell::RefCell;
  2. fn main() {
  3. let current = RefCell::new(String::from("草稿"));
  4. let old = current.replace(String::from("定稿"));
  5. println!("舊值:{},新值:{}", old, current.borrow());
  6. let left = RefCell::new(vec![1, 2]);
  7. let right = RefCell::new(vec![9]);
  8. left.swap(&right);
  9. println!("left={:?} right={:?}", left.borrow(), right.borrow());
  10. }

輸出:
  1. 舊值:草稿,新值:定稿
  2. left=[9] right=[1, 2]

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

本版新增的棄用 API


以下 API 從 Rust 1.24 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
std::fmtFormatter::flagsuse the sign_plus, sign_minus, alternate, or sign_aware_zero_pad methods instead

修補版本


版本發布日期含安全修補內容(官方原文)
1.24.12018-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-5222cargoCargo can be coerced to share credentials between registries早於 1.96.01.96.0
CVE-2026-5223cargoCrates in third party registries can override the cached source of other crates早於 1.96.01.96.0
CVE-2024-43402stdstd::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods早於 1.81.01.81.0
CVE-2024-24576stdstd::process::Command did not properly escape arguments of batch files on Windows早於 1.77.21.77.2
CVE-2023-38497cargoCargo not respecting umask when extracting crate archives早於 1.71.11.71.1
CVE-2022-46176cargoCargo did not verify SSH host keys早於 1.66.11.66.1
CVE-2022-36113cargoExtracting malicious crates can corrupt arbitrary files早於 1.64.01.64.0
CVE-2022-36114cargoExtracting malicious crates can fill the file system早於 1.64.01.64.0
CVE-2022-21658stdRace condition in std::fs::remove_dir_all早於 1.58.11.58.1
CVE-2021-42574rustcrustc and bidirectional-override codepoints in source code早於 1.56.11.56.1
CVE-2021-28878stdZip may call __iterator_get_unchecked twice with the same index1.14.0 起,早於 1.52.01.52.0
CVE-2021-28879stdZip can cause buffer overflow when a consumed Zip iterator is used again1.14.0 起,早於 1.52.01.52.0
CVE-2021-28876stdPanic safety issue in Zip specialization1.14.0 起,早於 1.52.01.52.0
CVE-2021-28875stdLogic bug in Read can cause buffer overflow in read_to_end()1.20.0 起,早於 1.50.01.50.0
CVE-2021-28877stdTrustedRandomAccess specialization composes incorrectly for nested iter::Zips1.11.0 起,早於 1.51.01.51.0
CVE-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0
CVE-2019-16760cargoCargo prior to Rust 1.26.0 may download the wrong dependency早於 1.26.01.26.0
CVE-2019-1010299stdvec_deque::Iter has unsound Debug implementation1.18.0 起,早於 1.30.01.30.0
CVE-2018-1000622rustdocUncontrolled search path element vulnerability in rustdoc plugins早於 1.27.11.27.1
CVE-2018-25008stdInsufficient synchronization in Arc::get_mut1.3.0 起,早於 1.29.01.29.0

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

資料來源