Rust 1.30

最後更新


Rust 1.30 發布於 2018-10-25,目前最新修補版為 1.30.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.30 讓程序式巨集(procedural macro)正式穩定,#[derive] 以外的屬性巨集與函式式巨集都能在 stable 上撰寫。同一版也為即將到來的 2018 edition 鋪路:路徑可以用 crate:: 開頭、外部 crate 不必再寫 :: 前綴、巨集可以用 use 匯入,還新增了原始識別字 r#。對新手來說,這版之後「用 use 匯入巨集」與「crate:: 表示自己這個 crate」就是日常寫法。

語言變更


  • 程序式巨集正式可用,屬性巨集與函式式巨集都能自訂,詳見 程序式巨集。
  • 原始識別字(raw identifier)r# 讓關鍵字也能當作名稱,例如 let r#for = true;。
  • 路徑可以用 crate 開頭指向 crate 根,use crate::foo; 就是 src/lib.rs 裡的 foo,詳見 模組系統。
  • 使用外部 crate 不再需要 :: 前綴,::serde_json::from_str 可以直接寫 serde_json::from_str。
  • 其他 crate 匯出的巨集可以用 use 匯入與重新匯出;#[macro_export] 的巨集會放在 crate 根模組,詳見 宣告式巨集。
  • 巨集新增 vis 片段,可以比對 pub、pub(crate) 這類可見性關鍵字。
  • #[used] 讓 static 即使看似沒用到也不會被編譯器最佳化掉;#[panic_handler] 可以自訂 panic 的處理函式。
  • trait 方法使用匿名參數改為棄用警告,在 2018 edition 會成為錯誤。

標準庫重點


  • Iterator::find_map 找到第一個轉換成功的元素,詳見 std::iter。
  • 新增 str::trim_start、str::trim_end 與 _matches 版本,用來取代 trim_left 系列,詳見 str。
  • Ipv4Addr::LOCALHOST、Ipv4Addr::UNSPECIFIED、Ipv4Addr::BROADCAST、Ipv6Addr::LOCALHOST、Ipv6Addr::UNSPECIFIED 常數穩定,詳見 std::net。
  • ManuallyDrop 的內部型別可以是大小不定(unsized)的型別。

Cargo 與工具


  • 在工作區裡執行 cargo run 不必再指定套件。
  • 建置時會顯示進度條;cargo doc 支援 --message-format=json。
  • rustdoc 新增 --edition、--color、--error-format 選項。
  • rustfmt、clippy 等工具的屬性可以使用,例如 #[rustfmt::skip] 會略過下一個項目的格式化。

原始識別字 r#


Rust 的關鍵字不能直接當變數或函式名稱。加上 r# 前綴後,編譯器就把它當成一般的名稱。日常很少需要,但跨 edition 合作時特別有用:2018 edition 新增了 async、dyn 等關鍵字,舊 crate 用這些字命名的函式,新程式可以用 r#async 呼叫。
範例:
  1. fn r#match(r#type: &str) -> bool {
  2. r#type == "fn"
  3. }
  4. fn main() {
  5. let r#for = "for";
  6. println!("{}", r#for);
  7. println!("{}", r#match("fn"));
  8. println!("{}", r#match("struct"));
  9. }

輸出:
  1. for
  2. true
  3. false

Iterator::find_map


find_map 等於「filter_map 之後取第一個」:對每個元素執行閉包,回傳第一個 Some,全部都是 None 就回傳 None。適合「從一串輸入中找出第一個能解析的值」。範例同時示範本版新增、用來取代 trim_left 的 trim_start 系列。
範例:
  1. fn main() {
  2. let inputs = ["apple", "42", "banana", "7"];
  3. let first_number = inputs.iter().find_map(|s| s.parse::<i32>().ok());
  4. println!("{:?}", first_number);
  5. let none: Option<i32> = ["x", "y"].iter().find_map(|s| s.parse().ok());
  6. println!("{:?}", none);
  7. let padded = " hello ";
  8. println!("[{}]", padded.trim_start());
  9. println!("[{}]", padded.trim_end());
  10. println!("[{}]", "xxabcxx".trim_start_matches('x'));
  11. }

輸出:
  1. Some(42)
  2. None
  3. [hello ]
  4. [ hello]
  5. [abcxx]

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.30.12018-11-08否Fixed overflow ICE in rustdoc;Cap Cargo progress bar width at 60 in MSYS terminals

已知漏洞


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

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

資料來源