Rust 1.33

最後更新


Rust 1.33 發布於 2019-02-28,目前最新修補版為 1.33.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.33 大幅擴充 const fn 能做的事,並穩定了「釘選」(pinning)的核心型別 Pin 與 Unpin,為後來的 async/await 打下基礎。模式比對也更好用:if let、while let 可以用 | 寫多個模式,整數的 match 只要涵蓋全部範圍就不必再加 _ 分支。升級時要注意:str::trim_left 系列與 Error::cause 從這版開始產生棄用警告。

語言變更


  • if let 與 while let 可以用 | 列出多個模式,語法與 match 相同,詳見 模式。
  • 整數模式可以窮盡:u8 的 match 涵蓋 0..=255 就不需要 _ => unreachable!(),詳見 match。
  • const fn 中可以使用 let 綁定、可變綁定、賦值、運算式陳述句與不可反駁的解構,也能呼叫 unsafe const fn,詳見 常數。
  • 可以用 use std::io::Read as _; 匯入 trait 的方法而不佔用名稱。
  • Rc、Arc、Pin 可以作為方法的接收者(self: Rc<Self>)。
  • #[repr(packed(N))] 可以指定對齊值;cfg_attr 可以一次寫多個屬性;新增 cfg(target_vendor)。
  • 不可反駁的 if let 與 while let(例如 if let _ = 5 {})可以編譯,但預設會產生警告。

標準庫重點


  • 穩定 pin::Pin、marker::Unpin、marker::PhantomPinned,詳見 std::pin。
  • Option::transpose 與 Result::transpose 在 Option<Result<T, E>> 與 Result<Option<T>, E> 之間互換,詳見 Option。
  • Vec::resize_with 與 VecDeque::resize_with 用閉包產生新元素,詳見 Vec。
  • Duration::as_millis、as_micros、as_nanos 回傳 u128 的總量,詳見 std::time。
  • 新增 convert::identity;Unix 的 FileExt::read_exact_at 與 write_all_at 穩定。
  • 整數的 overflowing_*、wrapping_*、rotate_left、count_ones、leading_zeros 等方法與 Ipv4Addr::new 改為 const fn。

Cargo 與工具


  • cargo publish 支援 --features 與 --all-features。
  • 檔案在第一次建置過程中被修改時,Cargo 會重新建置該 crate。

相容性注意


  • libtest 在 --test-threads=1 時不再為每個測試開新執行緒,並以固定順序執行測試。

if let 的多重模式


以前 if let 只能寫一個模式,想同時處理兩個列舉成員就得改用 match。現在可以用 | 連接多個模式,只要每個模式綁定相同名稱、相同型別的變數即可。
範例:
  1. enum Shape {
  2. Circle(f64),
  3. Square(f64),
  4. Point,
  5. }
  6. fn main() {
  7. let shapes = [Shape::Circle(1.5), Shape::Square(2.0), Shape::Point];
  8. for s in &shapes {
  9. if let Shape::Circle(size) | Shape::Square(size) = s {
  10. println!("有尺寸:{}", size);
  11. } else {
  12. println!("沒有尺寸");
  13. }
  14. }
  15. }

輸出:
  1. 有尺寸:1.5
  2. 有尺寸:2
  3. 沒有尺寸

窮盡的整數 match


編譯器現在會檢查整數範圍是否完全涵蓋。u8 只有 0 到 255,三個分支剛好涵蓋全部,就不再要求 _ 分支;如果漏掉某段範圍,編譯器會指出缺少哪些值。
範例:
  1. fn describe(b: u8) -> &'static str {
  2. match b {
  3. 0 => "零",
  4. 1..=127 => "ASCII 範圍",
  5. 128..=255 => "高位元組",
  6. }
  7. }
  8. fn main() {
  9. for b in [0u8, 65, 200] {
  10. println!("{} {}", b, describe(b));
  11. }
  12. }

輸出:
  1. 0 零
  2. 65 ASCII 範圍
  3. 200 高位元組

Option::transpose 與其他新 API


transpose 把「可能沒有的結果」翻轉成「可能失敗的可選值」,搭配 ? 處理 Option<Result<_, _>> 特別方便。範例同時示範 Duration::as_millis 與 Vec::resize_with。
範例:
  1. use std::time::Duration;
  2. fn parse_all(inputs: &[&str]) -> Option<Result<i32, std::num::ParseIntError>> {
  3. inputs.first().map(|s| s.parse::<i32>())
  4. }
  5. fn main() {
  6. let r = parse_all(&["10"]).transpose();
  7. println!("{:?}", r);
  8. let empty = parse_all(&[]).transpose();
  9. println!("{:?}", empty);
  10. let d = Duration::from_secs(2);
  11. println!("{} ms", d.as_millis());
  12. let mut v = vec![1, 2];
  13. let mut next = 10;
  14. v.resize_with(5, || {
  15. next += 1;
  16. next
  17. });
  18. println!("{:?}", v);
  19. }

輸出:
  1. Ok(Some(10))
  2. Ok(None)
  3. 2000 ms
  4. [1, 2, 11, 12, 13]

本版新增的棄用 API


以下 API 從 Rust 1.33 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdstr::trim_leftsuperseded by trim_start
stdstr::trim_left_matchessuperseded by trim_start_matches
stdstr::trim_rightsuperseded by trim_end
stdstr::trim_right_matchessuperseded by trim_end_matches
std::errorError::causereplaced by Error::source, which can support downcasting
std::stringString::trim_leftsuperseded by trim_start
std::stringString::trim_left_matchessuperseded by trim_start_matches
std::stringString::trim_rightsuperseded by trim_end
std::stringString::trim_right_matchessuperseded by trim_end_matches

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源