Rust 1.42

最後更新


Rust 1.42 發布於 2020-03-12,目前最新修補版為 1.42.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.42 擴充了切片模式,可以用 .. 比對「剩下的部分」並用 rest @ .. 綁定;新增的 matches! 巨集把「值是否符合某個模式」寫成一個布林運算式。Option 與 Result 的 unwrap、expect 在 panic 時,訊息會指向你呼叫它的那一行,而不是標準庫內部。升級時要注意:Error::description 已棄用,請改用 Display 或 to_string。

語言變更


  • 切片模式支援子切片,例如 [first, .., last] 與 [head, rest @ ..],詳見 模式。
  • #[repr(transparent)] 可以用在只有一個成員的列舉。
  • 外層屬性程序巨集可以用在行內模組上。
  • 幾項僅語法層面的變更(如 impl 中省略主體的項目),語意上仍會被拒絕,但程序巨集與條件編譯可以解析。

標準庫重點


  • matches! 巨集穩定,支援 | 與 if 守衛,詳見 match。
  • Option::{expect, unwrap} 與 Result::{expect, expect_err, unwrap, unwrap_err} 的 panic 訊息指向呼叫的位置,詳見 panic。
  • Condvar::wait_while、Condvar::wait_timeout_while(官方發布說明寫作 CondVar) 在條件成立時持續等待,詳見 共享狀態。
  • DebugMap::key、DebugMap::value、ManuallyDrop::take、ptr::slice_from_raw_parts、ptr::slice_from_raw_parts_mut 穩定。
  • io::Cursor 實作 PartialEq 與 Eq;iter::Empty<T> 對任何 T 都實作 Send 與 Sync;Layout::new 改為 const fn。

Cargo 與工具


  • 2018 edition 中 use proc_macro; 不再需要先寫 extern crate proc_macro;,詳見 程序式巨集。

相容性注意


  • Error::description 已棄用,使用時會產生警告,詳見 自訂錯誤。
  • 32 位元 Apple 目標降級,不再是 tier 1。

子切片模式


以前切片模式只能比對固定長度,想支援任意長度就得寫很多分支。現在 .. 代表「中間任意個元素」,rest @ .. 則把那段綁定成一個子切片。範例用一個 match 處理空切片、單一元素與多個元素三種情況。
範例:
  1. fn describe(words: &[&str]) -> String {
  2. match words {
  3. [] => String::from("空的"),
  4. [only] => format!("只有 {}", only),
  5. [first, .., last] => format!("從 {} 到 {}", first, last),
  6. }
  7. }
  8. fn main() {
  9. println!("{}", describe(&[]));
  10. println!("{}", describe(&["a"]));
  11. println!("{}", describe(&["a", "b", "c", "d"]));
  12. if let [head, rest @ ..] = [1, 2, 3].as_slice() {
  13. println!("{} {:?}", head, rest);
  14. }
  15. }

輸出:
  1. 空的
  2. 只有 a
  3. 從 a 到 d
  4. 1 [2, 3]

matches! 巨集


matches!(值, 模式) 在值符合模式時回傳 true,否則回傳 false,等於只有兩個分支的 match。它可以加上 if 守衛,也能用 | 組合多個模式,放在 filter 這類需要布林值的地方特別順手。
範例:
  1. #[derive(Debug)]
  2. enum Status {
  3. Active(u32),
  4. Paused,
  5. Closed,
  6. }
  7. fn main() {
  8. let list = [Status::Active(3), Status::Paused, Status::Active(0), Status::Closed];
  9. let busy = list
  10. .iter()
  11. .filter(|s| matches!(s, Status::Active(n) if *n > 0))
  12. .count();
  13. println!("{}", busy);
  14. println!("{}", matches!('x', 'a'..='z' | 'A'..='Z'));
  15. println!("{:?}", list.last());
  16. }

輸出:
  1. 1
  2. true
  3. Some(Closed)

本版新增的棄用 API


以下 API 從 Rust 1.42 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
std::errorError::descriptionuse the Display impl or to_string()

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源