Rust 1.35

最後更新


Rust 1.35 發布於 2019-05-23,目前最新修補版為 1.35.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.35 讓 Box<dyn FnOnce>、Box<dyn FnMut>、Box<dyn Fn> 直接實作對應的閉包 trait,裝箱的閉包可以像一般函式一樣呼叫或傳遞。範圍型別新增 contains,dbg!() 也能不帶參數使用,只印出所在的檔案與行號。這版沒有破壞性變更,Rust 工具鏈也開始原生支援以 musl 為基礎的 Linux 發行版。

語言變更


  • FnOnce、FnMut、Fn 分別為 Box<dyn FnOnce>、Box<dyn FnMut>、Box<dyn Fn> 實作,詳見 閉包 與 Box。
  • 閉包可以強制轉型為 unsafe fn 函式指標,詳見 unsafe。

標準庫重點


  • Range、RangeFrom、RangeTo、RangeInclusive、RangeToInclusive 新增 contains,詳見 std::ops。
  • f32::copysign、f64::copysign 把一個數的正負號套到另一個數上,詳見 f64。
  • Option::copied 把 Option<&T> 轉成 Option<T>(T 需實作 Copy),詳見 Option。
  • RefCell::replace_with 與 RefCell::map_split 穩定,詳見 std::cell;新增 ptr::hash。
  • dbg!() 不帶參數時只印出檔案與行號,方便追蹤程式走了哪個分支。
  • Thread 的 Debug 輸出包含執行緒 ID;{:#?} 展開格式的最後一個欄位後面會加逗號。
  • 所有 NonZero 數值型別實作 FromStr;str::make_ascii_lowercase 等就地 ASCII 大小寫轉換最快提升 4 倍。

Cargo 與工具


  • 建置腳本可以設定 cargo:rustc-cdylib-link-arg,在建置 cdylib 時傳遞自訂連結器參數。
  • 新增 wasm32-unknown-wasi 目標。

Range::contains


判斷數值是否落在某個區間,以前要寫 x >= 18 && x < 65,現在可以直接問範圍 (18..65).contains(&x)。半開區間 a..b 不含 b,閉區間 a..=b 含 b,只有上界的 ..b 也能用。參數是參考,所以要寫 &age。
範例:
  1. fn main() {
  2. let ages = 18..65;
  3. for age in [10, 18, 64, 65] {
  4. println!("{} {}", age, ages.contains(&age));
  5. }
  6. println!("{}", (1..=10).contains(&10));
  7. println!("{}", (..0).contains(&-3));
  8. }

輸出:
  1. 10 false
  2. 18 true
  3. 64 true
  4. 65 false
  5. true
  6. true

直接呼叫 Box<dyn FnOnce>


想把「不同的閉包」放進同一個 Vec,就要用 trait 物件 Box<dyn FnOnce()>。以前呼叫裝箱的 FnOnce 需要繞路,現在直接 task() 就能執行;Box<dyn Fn> 也能直接傳給 map 這類要求閉包的方法。
範例:
  1. fn main() {
  2. let name = String::from("Ferris");
  3. let tasks: Vec<Box<dyn FnOnce() -> String>> = vec![
  4. Box::new(move || format!("hello {}", name)),
  5. Box::new(|| String::from("bye")),
  6. ];
  7. for task in tasks {
  8. println!("{}", task());
  9. }
  10. let double: Box<dyn Fn(i32) -> i32> = Box::new(|x| x * 2);
  11. let result: Vec<i32> = [1, 2, 3].iter().copied().map(double).collect();
  12. println!("{:?}", result);
  13. }

輸出:
  1. hello Ferris
  2. bye
  3. [2, 4, 6]

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.35.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
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-2020-36323stdAPI soundness issue in join() implementation of [Borrow<str>]1.28.0 起,早於 1.52.01.52.0
CVE-2020-36317stdString::retain allows safely creating invalid strings when abusing panic1.26.0 起,早於 1.49.01.49.0
CVE-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0

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

資料來源