Rust 1.37

最後更新


Rust 1.37 發布於 2019-08-15,目前最新修補版為 1.37.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.37 讓列舉成員可以透過型別別名存取,並新增名稱為 _ 的匿名常數,方便巨集產生不會撞名的項目。Cargo 內建了 cargo vendor,可以把所有相依的原始碼下載到專案的 vendor/ 目錄;rustc 也支援以 -C profile-generate、-C profile-use 進行剖析導引最佳化(PGO)。升級時要注意:範圍模式使用 ... 與 trait 物件省略 dyn 從這版起預設產生警告。

語言變更


  • 可以透過型別別名使用列舉成員,例如 type MyOption = Option<u8>; 之後寫 MyOption::Some(y),詳見 列舉。
  • 常數可以用 _ 命名,例如 const _: u32 = 5;,同一個作用域可以有多個。
  • #[repr(align(N))] 可以用在列舉上。
  • cfg 與 cfg_attr 可以用在泛型參數上。
  • #[must_use] 型別被包在元組、Box 或陣列中而未使用時也會警告。
  • 巨集的 ? 重複運算子在 2015 edition 也能使用。

標準庫重點


  • Option::xor:兩者恰好一個是 Some 時回傳它,否則回傳 None,詳見 Option。
  • slice::copy_within 在同一個切片內複製一段元素,詳見 切片。
  • 所有整數與 Wrapping 新增 reverse_bits,反轉位元順序。
  • BufReader::buffer、BufWriter::buffer 查看緩衝區內容,詳見 std::io。
  • Cell::from_mut、Cell<[T]>::as_slice_of_cells 與 DoubleEndedIterator::nth_back 穩定。
  • mem::MaybeUninit<T> 與 T 的 ABI 相容。

Cargo 與工具


  • 內建 cargo vendor,把相依下載到 vendor/ 並印出建置時使用它們的設定。
  • 在 [package] 寫 default-run = "foo" 可以指定 cargo run 預設執行的二進位檔,詳見 Cargo 專案。
  • 發佈含可執行檔的 crate 時,預設會一併包含 Cargo.lock。

相容性注意


  • 包含範圍模式使用 ... 會預設警告,請改用 ..=。
  • trait 物件沒有寫 dyn 會預設警告,請改寫成 dyn Trait,詳見 trait 物件。

透過型別別名使用列舉成員


為很長的泛型型別取別名很常見,例如 type Reply = Result<u32, String>;。以前別名只能用在型別位置,建構或比對成員時仍要寫回 Result::Ok;現在可以直接寫 Reply::Ok、Reply::Err,讓程式碼與別名一致。
範例:
  1. type Reply = Result<u32, String>;
  2. fn show(r: Reply) -> String {
  3. match r {
  4. Reply::Ok(n) => format!("成功 {}", n),
  5. Reply::Err(e) => format!("失敗 {}", e),
  6. }
  7. }
  8. fn main() {
  9. println!("{}", show(Reply::Ok(7)));
  10. println!("{}", show(Reply::Err(String::from("timeout"))));
  11. }

輸出:
  1. 成功 7
  2. 失敗 timeout

匿名常數與其他新 API


const _ 宣告一個不需要名字的常數,常見用途是在編譯期做檢查:範例中如果 u64 不是 8 個位元組,程式根本無法編譯。範例也示範 Option::xor、copy_within(把索引 0..2 複製到索引 3 開始的位置)與 reverse_bits。
範例:
  1. const _: () = assert!(std::mem::size_of::<u64>() == 8);
  2. fn main() {
  3. println!("{:?}", Some(1).xor(None::<i32>));
  4. println!("{:?}", Some(1).xor(Some(2)));
  5. let mut buf = [1, 2, 3, 4, 5];
  6. buf.copy_within(0..2, 3);
  7. println!("{:?}", buf);
  8. println!("{:08b}", 0b0000_0110u8.reverse_bits());
  9. }

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

注意:範例的 assert! 寫在常數裡,需要 Rust 1.57 起支援的 const panic;1.37 推出時的匿名常數主要搭配巨集使用。

本版新增的棄用 API


以下 API 從 Rust 1.37 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
std::os::unix::processCommandExt::before_execshould be unsafe, use pre_exec instead

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源