Rust 1.44

最後更新


Rust 1.44 發布於 2020-06-04,目前最新修補版為 1.44.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.44 是一個小版本,官方公告的兩大重點是 Cargo 內建 cargo tree,以樹狀圖顯示整個相依關係,以及 async/.await 可以在 #![no_std] 環境中使用。標準庫新增 Vec<T> 從陣列建立的轉換與 PathBuf 的容量管理方法。升級時要注意:下一版(1.45)起,f64 以 as 轉成整數會改為飽和轉換,需要舊行為時改用新穩定的 to_int_unchecked。

語言變更


  • async/.await 可以在 #![no_std] 中使用,詳見 async 基礎。
  • 新增 unused_braces lint,提醒不必要的大括號。
  • 被 cfg 排除的外部模組(mod baz;)即使檔案不存在也能解析,語意上仍會被拒絕。

標準庫重點


  • Vec<T> 實作 From<[T; N]>(N 最多 32),詳見 Vec。
  • vec![] 直接對應 Vec::new(),可以用在 const 情境中。
  • PathBuf 新增 with_capacity、capacity、clear、reserve、reserve_exact、shrink_to_fit,詳見 std::path。
  • f32::to_int_unchecked、f64::to_int_unchecked 不檢查範圍地轉成整數,詳見 f64。
  • Layout::align_to、pad_to_align、array、extend 穩定。
  • String 實作 From<&mut str>;OsString 實作 DerefMut 與 IndexMut;IoSlice 實作 Copy;convert::Infallible 實作 Hash。
  • 整數的 from_le_bytes、to_le_bytes 等位元組轉換方法改為 const fn;支援 Unicode 13。

Cargo 與工具


  • 新增 cargo tree 顯示相依樹;cargo tree -d(--duplicates)列出同一個 crate 的多個版本,詳見 開發工具。
  • rustdoc 可以用 --crate-version 在側邊欄顯示版本。
  • 增量編譯模式下 rustc 會遵守 -C codegen-units,預設使用 256 個 codegen unit。

相容性注意


  • mem::zeroed 與 mem::uninitialized 用在不允許全零初始化的型別(如 NonZeroU8)時改為 panic,以前只是警告。
  • Windows GNU 目標產生的靜態函式庫副檔名改為 .a,不再是 .lib。
  • 移除 rustc 的 -C no_integrated_as 旗標;外部 LLVM 最低版本提高到 LLVM 8。

Vec::from 陣列與 const 的 vec![]


Vec::from([3, 1, 2]) 直接把陣列轉成向量,不必先轉切片再 to_vec()。String::from 也能接受 &mut str。因為空的 vec![] 會直接展開成 Vec::new(),它可以寫在 const 裡。
範例:
  1. fn main() {
  2. let v: Vec<i32> = Vec::from([3, 1, 2]);
  3. println!("{:?}", v);
  4. let mut owned = String::from("abc");
  5. let s: String = String::from(owned.as_mut_str());
  6. println!("{}", s);
  7. const EMPTY: Vec<u8> = vec![];
  8. println!("{}", EMPTY.len());
  9. }

輸出:
  1. [3, 1, 2]
  2. abc
  3. 0

PathBuf 容量管理與 to_int_unchecked


PathBuf 的內部是可增長的緩衝區,現在可以像 String 一樣預先配置容量、清空後重複使用。範例後半示範 to_int_unchecked 與 1.45 起的飽和轉換:300.0 as u8 得到上限 255、-1.0 as u8 得到下限 0,不再是未定義行為。
範例:
  1. use std::path::PathBuf;
  2. fn main() {
  3. let mut path = PathBuf::with_capacity(64);
  4. println!("{}", path.capacity() >= 64);
  5. path.push("logs");
  6. path.push("app.log");
  7. println!("{}", path.display());
  8. path.clear();
  9. println!("[{}]", path.display());
  10. let x = 3.9_f64;
  11. let n: i32 = unsafe { x.to_int_unchecked() };
  12. println!("{}", n);
  13. println!("{} {}", 300.0_f64 as u8, -1.0_f64 as u8);
  14. }

輸出:
  1. true
  2. logs/app.log
  3. []
  4. 3
  5. 255 0

注意:to_int_unchecked 是 unsafe 方法,值是 NaN、無窮大或超出目標型別範圍時屬於未定義行為;只在已確定值落在範圍內、且需要極致效能時使用,一般轉換用 as 或 TryFrom。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.44.12020-06-18否rustfmt accepts rustfmt_skip in cfg_attr again.;Don't hash executable filenames on apple platforms, fixing backtraces.;Fix crashes when finding backtrace on macOS.;Clippy applies lint levels into different files.

已知漏洞


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

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

資料來源