Rust 1.47

最後更新


Rust 1.47 發布於 2020-10-08,目前最新修補版為 1.47.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.47 沒有新的語言功能,最受期待的改變在標準庫:Debug、PartialEq 等特徵(trait)不再只實作到長度 32 的陣列,任何長度的陣列都能直接印出與比較。這是標準庫內部先用上 const generics 的成果,對新手來說,[0; 34] 印不出來這種奇怪的限制從此消失。同一版的 panic 回溯(backtrace)變得精簡,並升級到 LLVM 11。

語言變更


  • 沒有使用的閉包(closure)會產生警告,詳見 閉包。

標準庫重點


  • std/core 中的特徵對任意長度的陣列都有實作,不再限於長度小於 33,詳見 陣列。
  • 新增 Result::as_deref、Result::as_deref_mut,詳見 Result。
  • 新增 Range::is_empty、RangeInclusive::is_empty,詳見 運算子特徵。
  • 新增 Vec::leak、pointer::offset_from,以及常數 f32::TAU、f64::TAU(2π),詳見 Vec 與 f64。
  • 整數的 checked_*、saturating_* 與 char/u8 的 is_ascii_* 等方法可以在 const 中使用。
  • ops::RangeFull 與 ops::Range 實作 Default;panic::Location 實作 Copy、Eq、Hash、Ord 等特徵。

Cargo 與工具


  • build-dependencies 預設以 opt-level 0 編譯,可用 [profile.release.build-override] 覆寫,詳見 Cargo。
  • cargo help 會顯示指令的 man page;workspace.default-members 會遵守 workspace.exclude。
  • RUST_BACKTRACE 的輸出更精簡,只留下與錯誤相關的幾層;需要完整內容時設 RUST_BACKTRACE=full,詳見 backtrace。
  • 新增 -C control-flow-guard,在 Windows 開啟 Control Flow Guard;rustdoc 新增 Ayu 主題。

相容性注意


  • linux-gnu 目標至少需要核心 2.6.32 與 glibc 2.11。
  • 最低支援的 Emscripten 版本提高到 1.39.20;CloudABI 目標降為 tier 3。
  • macro_rules! 中程序式巨集的展開方式改變,不衛生(unhygienic)的巨集可能出現編譯錯誤。

任意長度陣列的特徵實作


陣列型別 [T; N] 把長度 N 寫在型別裡。以前標準庫只能為 0 到 32 每種長度各寫一份實作,長度 33 以上的陣列連 {:?} 都印不出來。現在 Debug、PartialEq、Clone 等特徵對任何長度都成立。
範例:
  1. fn main() {
  2. let big = [7u8; 40];
  3. let copy = big;
  4. println!("{}", big == copy);
  5. println!("{:?}", &big[..5]);
  6. let zeros = [0u16; 34];
  7. let total: u16 = zeros.iter().sum();
  8. println!("{} {:?}", total, zeros == [0; 34]);
  9. println!("{:?}", [1; 33]);
  10. }

輸出:
  1. true
  2. [7, 7, 7, 7, 7]
  3. 0 true
  4. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Result::as_deref 與 Range::is_empty


Result<String, E> 呼叫 as_deref() 會得到 Result<&str, &E>,不必移動原本的值就能用 &str 處理成功的結果。is_empty() 則回答「這個範圍有沒有任何元素」,例如 3..3 與 1..=0 都是空的。
範例:
  1. fn find_user(id: u32) -> Result<String, String> {
  2. if id == 1 {
  3. Ok(String::from("alice"))
  4. } else {
  5. Err(format!("user {id} not found"))
  6. }
  7. }
  8. fn main() {
  9. let found = find_user(1);
  10. let name: Result<&str, &String> = found.as_deref();
  11. println!("{:?}", name);
  12. let missing = find_user(9);
  13. println!("{:?}", missing.as_deref());
  14. println!("{:?}", (3..3).is_empty());
  15. println!("{:?}", (1..=0).is_empty());
  16. }

輸出:
  1. Ok("alice")
  2. Err("user 9 not found")
  3. true
  4. true

TAU 常數


TAU 等於 2π,也就是一整圈的弧度。計算圓周長或角度換算時,用 TAU 比 2.0 * PI 更直接。
範例:
  1. use std::f64::consts::{PI, TAU};
  2. fn main() {
  3. let r = 2.0;
  4. println!("{:.4}", TAU * r);
  5. println!("{}", TAU == 2.0 * PI);
  6. }

輸出:
  1. 12.5664
  2. true

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源