Rust 1.41

最後更新


Rust 1.41 發布於 2020-01-30,目前最新修補版為 1.41.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.41 放寬了孤兒規則(orphan rule),在泛型情境下可以替外部 trait 寫出以前被拒絕的實作,例如 impl<T> From<MyType<T>> for Vec<T>。Cargo 也有多項改進:cargo install 會更新過期的套件、Cargo.lock 改用較不易產生合併衝突的格式,並可以針對單一相依覆寫建置設定。升級時要注意:1.41 是最後一個以 tier 1 支援 32 位元 Apple 目標的版本。

語言變更


  • 孤兒規則放寬:實作外部 trait 時可以傳入型別參數,例如 impl<T> From<Foo> for Vec<T> {},詳見 trait 基礎。
  • self 的接收者型別可以任意巢狀,例如 fn foo(self: Box<Box<Self>>)。
  • format_args! 中可以使用任何合法識別字,包含底線開頭的名稱。
  • Box<T> 用在 extern "C" 函式時,C 端可以對應為 T*,詳見 FFI。
  • 可見性修飾詞(如 pub)在語法上可以出現在 trait 項目與列舉成員上,但語意上仍會被拒絕,主要供程序巨集與條件編譯解析。

標準庫重點


  • Result::map_or 與 Result::map_or_else 穩定,與 Option 的同名方法對應,詳見 Result。
  • rc::Weak 與 sync::Weak 新增 strong_count 與 weak_count,詳見 std::rc 與 std::sync。
  • 較小寬度的 NonZero* 可以用 From 轉成較大寬度,例如 NonZeroU16 實作 From<NonZeroU8>。
  • core::panic 模組穩定;MaybeUninit<T> 實作 fmt::Debug。

Cargo 與工具


  • cargo install 偵測到套件過期時會重新安裝。
  • Cargo.lock 改用對 git 更友善的格式,減少合併衝突。
  • 可以覆寫特定相依的建置設定,例如 [profile.dev.package.image] opt-level = 2;[profile.<profile>.build-override] 覆寫建置腳本,詳見 Cargo 專案。
  • 二進位 crate 的 cargo doc 預設會產生私有項目的文件。
  • 文件中的程式碼區塊可以標 edition2018 指定 edition;#[cfg(doc)] 讓項目只在產生文件時編譯,詳見 文件測試。
  • rustc 會警告未使用的迴圈標籤;可以用 @path 傳入參數檔。

相容性注意


  • 1.41 是最後一個以 tier 1 支援 32 位元 Apple 目標的版本,之後不再發佈這些平台的官方二進位檔。

放寬的孤兒規則


孤兒規則規定:實作 trait 時,trait 或型別至少要有一個屬於自己的 crate,避免兩個 crate 對同一組合寫出衝突的實作。以前只要牽涉泛型,impl<T> From<Wrapper<T>> for Vec<T> 這種「外部 trait、外部型別、但型別參數裡有自己的型別」的寫法會被拒絕;1.41 起編譯器能判斷它不會衝突,允許這個實作。
範例:
  1. struct Wrapper<T>(Vec<T>);
  2. impl<T> From<Wrapper<T>> for Vec<T> {
  3. fn from(w: Wrapper<T>) -> Vec<T> {
  4. w.0
  5. }
  6. }
  7. fn main() {
  8. let w = Wrapper(vec![1, 2, 3]);
  9. let v: Vec<i32> = w.into();
  10. println!("{:?}", v);
  11. }

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

Result::map_or 與 map_or_else


map_or(預設值, 閉包):Ok 時用閉包轉換值,Err 時回傳預設值。map_or_else(錯誤閉包, 成功閉包) 則讓兩邊都用閉包處理,錯誤閉包可以讀取錯誤內容。注意參數順序:錯誤處理在前,成功處理在後。
範例:
  1. fn main() {
  2. let ok: Result<i32, String> = Ok(4);
  3. let bad: Result<i32, String> = Err(String::from("壞掉"));
  4. println!("{}", ok.map_or(0, |n| n * 10));
  5. println!("{}", bad.clone().map_or(0, |n| n * 10));
  6. let text = bad.map_or_else(|e| format!("錯誤:{}", e), |n| n.to_string());
  7. println!("{}", text);
  8. }

輸出:
  1. 40
  2. 0
  3. 錯誤:壞掉

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.41.12020-02-27否Always check types of static items;Always check lifetime bounds of Copy impls;Fix miscompilation in callers of Layout::repeat;Rust 1.41.0 was announced as the last Rust release with tier 1 or tier 2 support for 32-bit Apple targets. That announcement did not expect a patch release. 1.41.1 also includes release binaries for these targets.

已知漏洞


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

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

資料來源