Rust 1.32

最後更新


Rust 1.32 發布於 2019-01-17,目前最新修補版為 1.32.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.32 帶來新手最實用的除錯工具 dbg! 巨集,同時把程式預設的記憶體配置器從 jemalloc 改為系統配置器。2018 edition 的模組路徑規則也定案:沒有 self、super、crate 前綴的路徑,會先解析成目前模組內的項目。升級時要注意:如果依賴 jemalloc 的效能,需要自行加入 jemallocator crate 並以 #[global_allocator] 指定。

語言變更


  • 2018 edition:巨集定義可以使用 ? 重複運算子,表示出現零次或一次。
  • 2018 edition:沒有前綴關鍵字的路徑會優先解析成模組內的項目,再找外部 crate 或 prelude,例如 use Color::*;,詳見 模組系統。
  • Self 可以當作單元結構體與元組結構體的建構子與模式,也能用在型別定義裡,詳見 方法。
  • 巨集新增 literal 片段,比對任何型別的字面值,例如 1、'A'、"Hello World",詳見 宣告式巨集。
  • 可以對 PhantomData<T> 做模式比對;trait 可以標上 #[must_use]。

標準庫重點


  • dbg! 巨集穩定,把運算式與值印到 stderr 並原樣回傳。
  • 所有整數型別新增 to_be_bytes、to_le_bytes、to_ne_bytes 與 from_be_bytes、from_le_bytes、from_ne_bytes,詳見 整數。
  • PathBuf 實作 FromStr;Box<[T]> 實作 FromIterator<T>。
  • Duration::as_secs、slice::as_ptr、iter::empty、Ipv6Addr::new 等函式改為 const fn。

Cargo 與工具


  • cargo c 是 cargo check 的別名。
  • 替代 registry 的 URL 可以包含使用者名稱。

相容性注意


  • 程式預設改用系統配置器;編譯器本身在 Linux 與 macOS 上仍使用 jemalloc。
  • AVX 的 _mm256_stream_si256、_mm256_stream_pd、_mm256_stream_ps 參數型別從 *const 改為 *mut,因為原本的實作不健全(unsound)。

dbg! 巨集


dbg! 會印出「檔名、行號、欄號、運算式原文與值」,然後把值原樣回傳,所以可以直接包在運算式中間而不影響邏輯。它寫到 stderr 而不是 stdout,不會混進程式正式的輸出。範例的輸出區塊中,前兩行來自 stderr,done 來自 stdout。
範例:
  1. fn area(w: u32, h: u32) -> u32 {
  2. dbg!(w * h)
  3. }
  4. fn main() {
  5. let w = 3;
  6. let total = area(w, 4) + 1;
  7. dbg!(total);
  8. println!("done");
  9. }

輸出:
  1. [main.rs:2:5] w * h = 12
  2. [main.rs:8:5] total = 13
  3. done

注意:dbg! 會取得參數的所有權再回傳;不想移動值時傳參考,例如 dbg!(&name)。

整數與位元組陣列互轉


處理網路協定或檔案格式時,常要把整數拆成位元組或反過來組合。to_be_bytes 以大端序(big-endian,高位在前)輸出,to_le_bytes 以小端序輸出;from_*_bytes 則從固定長度的陣列組回整數。同一組位元組用不同的位元組序解讀,會得到完全不同的值。
範例:
  1. fn main() {
  2. let n: u32 = 0x12345678;
  3. println!("{:02x?}", n.to_be_bytes());
  4. println!("{:02x?}", n.to_le_bytes());
  5. let bytes = [0x00, 0x00, 0x01, 0x00];
  6. println!("{}", u32::from_be_bytes(bytes));
  7. println!("{}", u32::from_le_bytes(bytes));
  8. }

輸出:
  1. [12, 34, 56, 78]
  2. [78, 56, 34, 12]
  3. 256
  4. 65536

以 Self 建構元組結構體


在 impl 區塊中,Self 現在可以當作元組結構體的建構子與模式,改名結構體時不必逐一修改方法內容。
範例:
  1. #[derive(Debug)]
  2. struct Point(i32, i32);
  3. impl Point {
  4. fn origin() -> Self {
  5. Self(0, 0)
  6. }
  7. fn is_origin(&self) -> bool {
  8. match self {
  9. Self(0, 0) => true,
  10. _ => false,
  11. }
  12. }
  13. }
  14. fn main() {
  15. let p = Point::origin();
  16. println!("{:?} {}", p, p.is_origin());
  17. let q = Point(1, 2);
  18. println!("{:?} {}", q, q.is_origin());
  19. }

輸出:
  1. Point(0, 0) true
  2. Point(1, 2) false

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.32.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。

資料來源