Rust 1.43

最後更新


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

版本介紹


Rust 1.43 是一個小版本,重點是巨集的 item 片段可以插入到 trait、impl 與 extern 區塊中,以及整數與浮點數的關聯常數可以直接使用:寫 u32::MAX、f32::NAN 不必再 use std::u32;。數值字面值搭配參考的型別推斷也修正了,Cargo 則為整合測試提供可執行檔路徑的環境變數。升級時要注意:assert! 後面多餘的語法從警告變成錯誤。

語言變更


  • 巨集的 item 片段可以插入 trait、impl 與 extern 區塊,詳見 宣告式巨集。
  • #[cfg()] 等屬性可以用在 if 運算式上。
  • 修正 &1.0 這類對數字取參考後進行二元運算時的型別推斷。
  • 幾項僅語法層面的變更(如 type Foo: Ord、所有 fn 中允許 self),語意上仍會被拒絕。

標準庫重點


  • 整數與浮點數的關聯常數可以直接使用,例如 u32::MAX、f32::NAN,詳見 整數 與 浮點數。
  • 新增 primitive 模組重新匯出原生型別,主要讓巨集避免型別名稱被遮蔽。
  • Once::is_completed 與 iter::once_with 穩定,詳見 std::sync 與 std::iter。
  • 新增常數 f32::consts::LOG10_2、LOG2_10 與 f64 的對應常數。
  • Arc<[T; N]>、Box<[T; N]>、Rc<[T; N]> 實作從對應切片型別的 TryFrom(N 為 0..=32)。
  • String 實作 AsMut<str>;FromUtf8Error 實作 Clone 與 Eq;u8::is_ascii 改為 const fn。

Cargo 與工具


  • .cargo/config 或環境變數可以設定 [profile]。
  • 執行整合測試或基準測試時,Cargo 設定 CARGO_BIN_EXE_<name> 指向二進位檔的路徑,可以用 env!("CARGO_BIN_EXE_my-program") 取得,詳見 整合測試。
  • rustc 可以傳入多個 lint 旗標,後面的旗標覆寫前面的,例如 -D unused -A unused-variables。

相容性注意


  • assert! 巨集後面有多餘語法改為硬錯誤,自 1.36 起它就是警告。
  • 修正 Self 型別推斷錯誤,部分以前誤被接受的程式碼現在會編譯失敗。

直接使用數值型別的關聯常數


以前取得 u32 的最大值要寫 std::u32::MAX(模組常數)或先 use std::u32;。1.43 起 MAX、MIN、EPSILON、NAN 等都是型別本身的關聯常數,直接寫 u32::MAX、f64::EPSILON 即可。數學常數如 LOG10_2 仍放在 std::f64::consts 模組裡。
範例:
  1. use std::f64::consts::LOG10_2;
  2. fn main() {
  3. println!("{}", u32::MAX);
  4. println!("{}", i8::MIN);
  5. println!("{}", f64::EPSILON > 0.0);
  6. println!("{}", f32::NAN.is_nan());
  7. println!("{:.4}", LOG10_2);
  8. }

輸出:
  1. 4294967295
  2. -128
  3. true
  4. true
  5. 0.3010

在 impl 中插入 item 片段


$x:item 片段比對一個完整的項目(函式、結構體等)。以前它只能放在模組層級,現在可以插進 impl、trait 與 extern 區塊,巨集能夠接收一個方法定義並放進產生的 impl 中。
範例:
  1. macro_rules! with_greet {
  2. ($name:ident, $method:item) => {
  3. struct $name;
  4. impl $name {
  5. $method
  6. }
  7. };
  8. }
  9. with_greet!(Robot, fn greet(&self) -> &'static str { "beep" });
  10. fn main() {
  11. println!("{}", Robot.greet());
  12. }

輸出:
  1. beep

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.43.12020-05-07是Updated openssl-src to 1.1.1g for CVE-2020-1967.;Fixed the stabilization of AVX-512 features.;Fixed cargo package --list not working with unpublished dependencies.

已知漏洞


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

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2020-1967vendoredUpdated openssl-src to 1.1.1g for CVE-2020-1967.未公布1.43.1

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

資料來源