Rust 1.46

最後更新


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

版本介紹


Rust 1.46 大幅擴充了 const fn 能做的事:if、match、loop、while 與 &&/|| 都能在編譯期執行,讓更多計算可以在編譯時完成。同一版穩定了 #[track_caller],自己寫的會 panic 的函式也能像 unwrap 一樣,把錯誤位置指向呼叫端。對新手來說,常數不再只能是簡單字面值;升級時要注意 Linux 上的 rustc 改為動態連結 libz。

語言變更


  • const fn 裡可以使用 if、if let、match、while、while let、loop 與 &&/||,也能轉型成切片 &[T],詳見 常數。
  • 函式可以加上 #[track_caller],panic 訊息會顯示呼叫者的位置,詳見 panic。
  • 巢狀的元組索引不必再加括號,x.0.0 等同 (x.0).0,詳見 元組與陣列。
  • mem::transmute 可以用在 static 與 const 中(仍不能用在 const fn)。

標準庫重點


  • 新增 Option::zip,把兩個 Option 合成一個元組,詳見 Option。
  • 新增 vec::Drain::as_slice;mem::forget 成為 const fn。
  • String 實作 From<char>;所有整數型別新增 leading_ones、trailing_ones,詳見 數值。
  • NonZeroU8 等非零整數型別實作從一般整數的 TryFrom。
  • &[T] 與 &mut [T] 實作 PartialEq<Vec<T>>;(String, u16) 實作 ToSocketAddrs。

Cargo 與工具


  • 編譯時新增環境變數 CARGO_BIN_NAME、CARGO_CRATE_NAME、CARGO_PKG_LICENSE、CARGO_PKG_LICENSE_FILE,詳見 Cargo。

相容性注意


  • 對實作 Drop 的 C 風格列舉做轉型會產生警告,未來將成為錯誤。
  • 結構體上的 #[repr(i128)]/#[repr(u128)] 會編譯失敗,這兩種表示法只允許用在列舉。
  • f32::asinh/f64::asinh 對負數改回傳正確結果。
  • Linux 上的 rustc 改為動態連結 libz,系統需要安裝該函式庫。

const fn 中的流程控制


const fn 是可以在編譯期執行的函式,結果能直接拿來初始化常數。這版起可以在裡面寫迴圈與分支,下面的費氏數列與成績等級都在編譯時就算好。
範例:
  1. const fn fib(n: u32) -> u64 {
  2. let mut a = 0;
  3. let mut b = 1;
  4. let mut i = 0;
  5. while i < n {
  6. let next = a + b;
  7. a = b;
  8. b = next;
  9. i += 1;
  10. }
  11. a
  12. }
  13. const fn grade(score: u32) -> char {
  14. match score {
  15. 90..=100 => 'A',
  16. 80..=89 => 'B',
  17. _ => if score >= 60 { 'C' } else { 'F' },
  18. }
  19. }
  20. const FIB_50: u64 = fib(50);
  21. const G: [char; 3] = [grade(95), grade(85), grade(42)];
  22. fn main() {
  23. println!("fib(50) = {FIB_50}");
  24. println!("{G:?}");
  25. }

輸出:
  1. fib(50) = 12586269025
  2. ['A', 'B', 'F']

注意:這一版開放的迴圈只有 while、while let 與 loop,所以範例用 while 手動計數,而不是 for。

#[track_caller]


加上 #[track_caller] 的函式,在裡面呼叫 std::panic::Location::caller() 會得到「呼叫這個函式的地方」,而不是函式本身的位置。標準庫的 unwrap 就是靠它讓 panic 訊息指向使用者的程式碼。
範例:
  1. use std::panic::Location;
  2. #[track_caller]
  3. fn checked_div(a: i32, b: i32) -> Option<i32> {
  4. if b == 0 {
  5. let loc = Location::caller();
  6. println!("除以零,呼叫位置在第 {} 行", loc.line());
  7. return None;
  8. }
  9. Some(a / b)
  10. }
  11. fn main() {
  12. println!("{:?}", checked_div(10, 2));
  13. println!("{:?}", checked_div(1, 0));
  14. println!("{:?}", checked_div(7, 0));
  15. }

輸出:
  1. Some(5)
  2. 除以零,呼叫位置在第 15 行
  3. None
  4. 除以零,呼叫位置在第 16 行
  5. None

Option::zip 與巢狀元組索引


a.zip(b) 在兩者都是 Some 時回傳 Some((a, b)),任一個是 None 就回傳 None,適合「兩個值都要有才繼續」的情境。巢狀元組現在可以直接寫 nested.1.0。
範例:
  1. fn main() {
  2. let width = Some(3);
  3. let height = Some(4);
  4. println!("{:?}", width.zip(height));
  5. println!("{:?}", width.zip(None::<i32>));
  6. let nested = ((1, 2), (3, 4));
  7. println!("{}", nested.1.0);
  8. }

輸出:
  1. Some((3, 4))
  2. None
  3. 3

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源