Rust 1.55

最後更新


Rust 1.55 發布於 2021-09-09,目前最新修補版為 1.55.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.55 讓模式可以寫「只有起點」的開放範圍,例如 1.. 代表 1 到該整數型別的最大值;陣列新增 map,可以直接把 [T; N] 轉成 [U; N]。標準庫的浮點數解析改用 Eisel-Lemire 演算法,更快也更正確,Cargo 會合併重複的編譯警告。升級時要注意:標準庫回傳的 io::Error 不再使用 ErrorKind::Other,比對錯誤種類時要保留 _ 分支。這版獻給創造「turbofish」(::<>)一詞的社群成員 Anna Harren。

語言變更


  • 模式可以使用開放的起點範圍 X..,詳見 模式。
  • 可以透過 std::prelude 明確匯入不同版次的 prelude,例如 use std::prelude::rust_2021::*;,詳見 prelude。

標準庫重點


  • 新增 array::map,詳見 陣列。
  • 新增 ops::ControlFlow,搭配 try_for_each 等方法提前結束走訪,詳見 運算子特徵。
  • 浮點數字串解析改用 Eisel-Lemire 演算法,部分以前被拒絕的合法值也能解析,詳見 f64。
  • 新增 Bound::cloned、string::Drain::as_str、IntoInnerError::into_error、IntoInnerError::into_parts。
  • 新增 MaybeUninit::write、assume_init_ref、assume_init_mut,詳見 std::mem。

Cargo 與工具


  • 平行編譯同一個 crate 時(如 cargo test)不再重複印出相同的警告與錯誤,詳見 Cargo。
  • 新增 cargo d 作為 cargo doc 的別名;cargo tree 可以用 {lib} 格式印出函式庫名稱。
  • cargo metadata 的套件資訊包含 default_run 欄位。
  • rustdoc 的特徵實作區塊預設展開,方便用瀏覽器搜尋。

相容性注意


  • 標準庫回傳的 io::Error 不再使用 ErrorKind::Other,未分類的錯誤改用內部的 Uncategorized,詳見 std::io。
  • 建置腳本不再取得 RUSTFLAGS,改用 CARGO_ENCODED_RUSTFLAGS。
  • Windows 上 process::Command 的環境變數名稱不再被轉成大寫。
  • 沒有 rustdoc:: 前綴的 rustdoc lint 會產生警告。

開放範圍模式


1.. 在模式中代表「1 以上」,和 0、1..=9 這類模式一起使用就能窮舉整個 u32,不必再寫 _ 兜底,觀念詳見 match。
範例:
  1. fn describe(n: u32) -> &'static str {
  2. match n {
  3. 0 => "zero",
  4. 1..=9 => "single digit",
  5. 10..=99 => "two digits",
  6. 100.. => "large",
  7. }
  8. }
  9. fn main() {
  10. for n in [0, 7, 42, 1000] {
  11. println!("{n}: {}", describe(n));
  12. }
  13. }

輸出:
  1. 0: zero
  2. 7: single digit
  3. 42: two digits
  4. 1000: large

注意:這版只開放 X..;1..10 這種不含終點的範圍模式要到 Rust 1.80 起才能使用。

array::map


arr.map(f) 對每個元素套用 f,回傳長度相同的新陣列,不必先轉成 Vec 再轉回來。
範例:
  1. fn main() {
  2. let celsius = [0.0, 25.0, 100.0];
  3. let fahrenheit = celsius.map(|c| c * 9.0 / 5.0 + 32.0);
  4. println!("{:?}", fahrenheit);
  5. let words = ["a", "bb", "ccc"];
  6. let lens: [usize; 3] = words.map(str::len);
  7. println!("{:?}", lens);
  8. }

輸出:
  1. [32.0, 77.0, 212.0]
  2. [1, 2, 3]

ops::ControlFlow


ControlFlow 有 Continue 與 Break 兩個變體,用來明確表達「繼續」或「停下並帶回一個值」。搭配 try_for_each,遇到第一個負數就停止走訪。
範例:
  1. use std::ops::ControlFlow;
  2. fn main() {
  3. let data = [3, 8, -1, 5];
  4. let result = data.iter().try_for_each(|&x| {
  5. if x < 0 {
  6. return ControlFlow::Break(x);
  7. }
  8. println!("ok {x}");
  9. ControlFlow::Continue(())
  10. });
  11. println!("{:?}", result);
  12. }

輸出:
  1. ok 3
  2. ok 8
  3. Break(-1)

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

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

資料來源