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 呼叫。範例:
- fn r#match(r#type: &str) -> bool {
- r#type == "fn"
- }
- fn main() {
- let r#for = "for";
- println!("{}", r#for);
- println!("{}", r#match("fn"));
- println!("{}", r#match("struct"));
- }
輸出:
- for
- true
- false
Iterator::find_map
find_map 等於「filter_map 之後取第一個」:對每個元素執行閉包,回傳第一個 Some,全部都是 None 就回傳 None。適合「從一串輸入中找出第一個能解析的值」。範例同時示範本版新增、用來取代 trim_left 的 trim_start 系列。範例:
- fn main() {
- let inputs = ["apple", "42", "banana", "7"];
- let first_number = inputs.iter().find_map(|s| s.parse::<i32>().ok());
- println!("{:?}", first_number);
- let none: Option<i32> = ["x", "y"].iter().find_map(|s| s.parse().ok());
- println!("{:?}", none);
- let padded = " hello ";
- println!("[{}]", padded.trim_start());
- println!("[{}]", padded.trim_end());
- println!("[{}]", "xxabcxx".trim_start_matches('x'));
- }
輸出:
- Some(42)
- None
- [hello ]
- [ hello]
- [abcxx]
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.30.1 | 2018-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-5222 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
| CVE-2024-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
| CVE-2023-38497 | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | 1.71.1 |
| CVE-2022-46176 | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | 1.66.1 |
| CVE-2022-36113 | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | 1.64.0 |
| CVE-2022-36114 | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | 1.64.0 |
| CVE-2022-21658 | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | 1.58.1 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-28878 | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28879 | std | Zip can cause buffer overflow when a consumed Zip iterator is used again | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28876 | std | Panic safety issue in Zip specialization | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28875 | std | Logic bug in Read can cause buffer overflow in read_to_end() | 1.20.0 起,早於 1.50.0 | 1.50.0 |
| CVE-2021-28877 | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起,早於 1.51.0 | 1.51.0 |
| CVE-2020-36323 | std | API soundness issue in join() implementation of [Borrow<str>] | 1.28.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2020-36317 | std | String::retain allows safely creating invalid strings when abusing panic | 1.26.0 起,早於 1.49.0 | 1.49.0 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。