Rust 1.54

最後更新


Rust 1.54 發布於 2021-07-29,目前最新修補版為 1.54.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.54 讓內建屬性的值可以呼叫函式型巨集,最常見的用法是 #![doc = include_str!("README.md")],直接把 README 當成 crate 文件。WebAssembly 的 wasm32 內建函式(含 SIMD)也正式穩定。對新手來說,最直接的好處是 1.52.1 關掉的增量編譯(incremental compilation)在這版重新預設開啟,修改後重新編譯又變快了。升級時要注意閉包中混用 Option 與 Result 的 ? 在型別推斷時不再被接受。

語言變更


  • 部分內建屬性的值可以使用巨集,例如 #[doc = concat!(...)]、#![doc = include_str!("README.md")],詳見 註解與文件。
  • const fn 中可以在不定大小的切片型別之間轉型。
  • impl Trait 可以帶多個彼此沒有存活關係的生命週期,詳見 生命週期。

標準庫重點


  • 新增 HashMap::into_keys、HashMap::into_values、BTreeMap::into_keys、BTreeMap::into_values,詳見 HashMap 與 BTreeMap。
  • 新增 VecDeque::binary_search、binary_search_by、binary_search_by_key 與 partition_point,詳見 集合。
  • 穩定 arch::wasm32 內建函式;io::ErrorKind 新增 OutOfMemory,詳見 std::io。
  • panic::panic_any 加上 #[track_caller],詳見 std::panic。

Cargo 與工具


  • 增量編譯重新預設開啟。
  • cargo tree 新增 --prune、--depth,以及 --edges no-proc-macro,詳見 開發工具。
  • 新增環境變數 CARGO_TARGET_TMPDIR,提供整合測試與基準測試存放暫存檔的目錄,詳見 整合測試。
  • rustfmt 會正確格式化巢狀的外部模組檔案,先前被忽略的檔案可能出現格式變更。

相容性注意


  • 型別需要推斷時,閉包中不再允許透過 ? 混用 Option 與 Result。
  • 不同分支建構子需要不同生命週期的不健全程式碼不再被接受。
  • std::arch 內建函式的常數參數檢查更嚴格,部分舊程式碼可能被拒絕。

HashMap 與 BTreeMap 的 into_keys/into_values


into_keys()、into_values() 會消耗整個映射,直接取得鍵或值的所有權,不必再寫 into_iter().map(|(k, _)| k)。範例用 BTreeMap 讓輸出依鍵排序。
範例:
  1. use std::collections::BTreeMap;
  2. fn main() {
  3. let mut stock = BTreeMap::new();
  4. stock.insert("apple", 3);
  5. stock.insert("banana", 5);
  6. let names: Vec<&str> = stock.clone().into_keys().collect();
  7. let counts: Vec<i32> = stock.into_values().collect();
  8. println!("{:?}", names);
  9. println!("{:?}", counts);
  10. }

輸出:
  1. ["apple", "banana"]
  2. [3, 5]

注意:HashMap 的 into_keys/into_values 順序不固定,需要穩定順序時先排序。

VecDeque 的二分搜尋


VecDeque 和切片一樣可以對已排序的內容做二分搜尋:找到回傳 Ok(索引),找不到回傳 Err(應插入的索引)。partition_point 則回傳第一個不符合條件的位置。
範例:
  1. use std::collections::VecDeque;
  2. fn main() {
  3. let mut q: VecDeque<i32> = VecDeque::from(vec![10, 20, 30, 40]);
  4. q.push_front(5);
  5. println!("{:?}", q.binary_search(&30));
  6. println!("{:?}", q.binary_search(&25));
  7. let pos = q.partition_point(|&x| x < 25);
  8. q.insert(pos, 25);
  9. println!("{:?}", q);
  10. }

輸出:
  1. Ok(3)
  2. Err(3)
  3. [5, 10, 20, 25, 30, 40]

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


影響 1.54.0 的已知漏洞共 10 個;在本版修正的共 0 個;仍影響最新修補版 1.54.0 的共 10 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。

仍影響 1.54.0


編號元件摘要(官方原文)影響範圍修正於
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

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

資料來源