Rust 1.45

最後更新


Rust 1.45 發布於 2020-07-16,目前最新修補版為 1.45.2,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.45 修好了一個從 2013 年就存在的健全性(soundness)問題:浮點數用 as 轉成整數時,超出範圍的值以前是未定義行為(undefined behavior),現在固定改為「飽和轉換」。同一版讓函式型程序式巨集(procedural macro)可以用在運算式、模式與陳述式位置,Rocket 等框架因此能在 stable 上編譯。對新手來說,300.0 as u8 這類寫法從此有確定的結果;升級時要注意,依賴舊行為的極少數效能敏感程式可改用 unsafe 的 to_int_unchecked。

語言變更


  • 超出範圍的浮點數轉整數改為飽和轉換:太大取最大值、太小取最小值、NaN 變 0,詳見 型別轉換。
  • 函式型程序式巨集可以出現在運算式、模式與陳述式位置,和 macro_rules! 巨集一樣,詳見 程序式巨集。
  • mem::Discriminant<T> 改用 T 自己的判別值型別,不再一律用 u64。

標準庫重點


  • char 可以搭配範圍型別逐一走訪碼位,例如 'a'..='z',詳見 char。
  • 新增 str::strip_prefix、str::strip_suffix,詳見 str。
  • 新增 Rc::as_ptr、Arc::as_ptr,以及 rc::Weak/sync::Weak 的 as_ptr、from_raw、into_raw,詳見 Rc。
  • 新增 BTreeMap::remove_entry,詳見 BTreeMap。
  • 有號整數新增 saturating_neg,所有整數新增 saturating_abs;原子整數型別新增 fetch_min、fetch_max、fetch_update,詳見 原子型別。
  • Box<[T]> 實作 From<[T; N]>;SocketAddr 系列實作 PartialOrd 與 Ord。

Cargo 與工具


  • Cargo 改用 embed-bitcode 旗標減少磁碟用量與編譯時間。
  • rustdoc 的 Markdown 支援刪除線 ~~文字~~。
  • 升級到 LLVM 10。

相容性注意


  • 用自己初始化自己的 static 不健全,現在會編譯錯誤。
  • Windows 上 f32::powi/f64::powi 的結果可能和以前有些微差異。
  • 透過 RUSTFLAGS 自行開啟 lto 可能和 Cargo 加上的 embed-bitcode=no 衝突,建議改用 Cargo profile 控制 LTO。

浮點數轉整數改為飽和轉換


as 是 Rust 的基本型別轉換運算子。1.44 以前,把放不下的浮點數轉成小整數會產生未定義行為,而且程式裡根本沒有 unsafe。現在結果固定:超過上限得到該型別最大值,低於下限得到最小值,NaN 得到 0,範圍內的值則捨去小數。
範例:
  1. fn main() {
  2. let values = [300.0_f32, -100.0, 42.9, f32::INFINITY];
  3. for v in values {
  4. println!("{v} as u8 = {}", v as u8);
  5. }
  6. let nan = "NaN".parse::<f32>().unwrap();
  7. println!("NaN as u8 = {}", nan as u8);
  8. println!("1e10 as i32 = {}", 1e10_f64 as i32);
  9. }

輸出:
  1. 300 as u8 = 255
  2. -100 as u8 = 0
  3. 42.9 as u8 = 42
  4. inf as u8 = 255
  5. NaN as u8 = 0
  6. 1e10 as i32 = 2147483647

注意:飽和轉換不會報錯,數值「被默默截斷」仍可能是邏輯錯誤;需要知道是否超出範圍時,先自行檢查範圍再轉換。

char 範圍


char 現在可以放進 ..、..= 等範圍,直接當迭代器(iterator)使用,常用來產生字母表或檢查字元區間。
範例:
  1. fn main() {
  2. let letters: String = ('a'..='e').collect();
  3. println!("{letters}");
  4. println!("{}", ('a'..='z').count());
  5. for c in 'x'..='z' {
  6. print!("{} ", c.to_ascii_uppercase());
  7. }
  8. println!();
  9. }

輸出:
  1. abcde
  2. 26
  3. X Y Z

strip_prefix 與 strip_suffix


想去掉字串開頭或結尾的固定文字時,strip_prefix/strip_suffix 回傳 Option<&str>:有該前綴(後綴)就是 Some(剩下的部分),沒有就是 None,不必自己算索引切片。
範例:
  1. fn main() {
  2. let url = "https://example.com/index.html";
  3. println!("{:?}", url.strip_prefix("https://"));
  4. println!("{:?}", url.strip_prefix("ftp://"));
  5. let name = "report.txt";
  6. match name.strip_suffix(".txt") {
  7. Some(stem) => println!("stem = {stem}"),
  8. None => println!("not a txt file"),
  9. }
  10. }

輸出:
  1. Some("example.com/index.html")
  2. None
  3. stem = report

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.45.12020-07-30否Fix const propagation with references.;rustfmt accepts rustfmt_skip in cfg_attr again.;Avoid spurious implicit region bound.;Install clippy on x.py install
1.45.22020-08-03否Fix bindings in tuple struct patterns;Fix track_caller integration with trait objects

已知漏洞


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

仍影響 1.45.2


編號元件摘要(官方原文)影響範圍修正於
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。

資料來源