Rust 1.39

最後更新


Rust 1.39 發布於 2019-11-07,目前最新修補版為 1.39.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.39 正式推出 async fn、async 區塊與 .await,非同步程式可以寫得像同步程式一樣直觀,是 Rust 發展中的重要里程碑。標準庫只提供 Future 等基礎型別,實際執行非同步任務需要搭配執行器(executor),通常由第三方 crate 提供。升級時要注意:舊借用檢查器接受、但 NLL 拒絕的程式碼,在 2018 edition 從警告變成錯誤;try! 巨集也開始產生棄用警告,請改用 ?。

語言變更


  • 可以用 async fn、async {}、async move {} 建立非同步函式與區塊,並在其中以 .await 等待結果,詳見 async 基礎。
  • 函式、閉包與函式指標的參數可以加上 cfg、cfg_attr、allow、warn、deny、forbid 等屬性。
  • match 的 if 守衛中,可以對以移動方式綁定的變數取共用參考,詳見 match。

標準庫重點


  • Vec::new、String::new、LinkedList::new 改為 const fn,可以用在 const 與 static 中,詳見 Vec。
  • str::len、[T]::len、str::as_bytes 以及 abs、wrapping_abs、overflowing_abs 改為 const fn。
  • Pin::into_inner 穩定,詳見 std::pin。
  • Instant::checked_duration_since 與 Instant::saturating_duration_since 穩定,詳見 std::time。

Cargo 與工具


  • git 相依只要同時指定 version 就能發佈。
  • --all 更名為 --workspace,--all 已棄用,詳見 工作區。
  • 測試執行檔可以傳入 --show-output,印出通過的測試的輸出。
  • 編譯器的錯誤訊息會裁切程式碼片段以符合終端機寬度。

相容性注意


  • 2018 edition 中,舊借用檢查器接受但 NLL 拒絕的程式碼改為錯誤;2015 edition 會在 1.40 跟進。
  • rustdoc 執行文件測試時需要 rustc 安裝在同一個目錄。
  • asinh(-0.0) 改為正確回傳 -0.0。

async 與 .await


async fn 呼叫後不會立刻執行,而是回傳一個 Future,代表「之後會算出的值」;在另一個 async 函式裡用 .await 等待它完成。範例中 compute() 被呼叫時只建立了 Future,所以先印出「尚未執行」,交給 block_on 輪詢(poll)後才真正計算出 26。
這裡的 block_on 是教學用的最小執行器,用到 Rust 1.68 起的 pin! 與 1.85 起的 Waker::noop(),不斷輪詢直到完成;實際專案會使用第三方 crate 提供的執行器,它們能在等待 I/O 時讓出執行緒。
範例:
  1. use std::future::Future;
  2. use std::pin::pin;
  3. use std::task::{Context, Poll, Waker};
  4. async fn add(a: i32, b: i32) -> i32 {
  5. a + b
  6. }
  7. async fn compute() -> i32 {
  8. let x = add(1, 2).await;
  9. let y = add(x, 10).await;
  10. y * 2
  11. }
  12. fn block_on<F: Future>(fut: F) -> F::Output {
  13. let mut fut = pin!(fut);
  14. let mut cx = Context::from_waker(Waker::noop());
  15. loop {
  16. if let Poll::Ready(v) = fut.as_mut().poll(&mut cx) {
  17. return v;
  18. }
  19. }
  20. }
  21. fn main() {
  22. let fut = compute();
  23. println!("尚未執行");
  24. println!("{}", block_on(fut));
  25. }

輸出:
  1. 尚未執行
  2. 26

注意:Future 是惰性的,建立後如果沒有被 .await 或交給執行器,裡面的程式碼永遠不會執行。

在 match 守衛中借用移動綁定


match data 會把 Box 移動到分支的 nums。以前在 if 守衛裡呼叫 nums.iter()(需要借用)會被拒絕;現在守衛可以先借用檢查條件,進入分支後再取得所有權,例如把 Box 解開成 Vec。
範例:
  1. fn main() {
  2. let data: Box<Vec<i32>> = Box::new(vec![1, 2, 3, 4]);
  3. match data {
  4. nums if nums.iter().sum::<i32>() == 10 => {
  5. let owned: Vec<i32> = *nums;
  6. println!("總和是 10:{:?}", owned);
  7. }
  8. _ => println!("其他"),
  9. }
  10. }

輸出:
  1. 總和是 10:[1, 2, 3, 4]

本版新增的棄用 API


以下 API 從 Rust 1.39 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdtry!use the ? operator instead
std::memuninitializeduse mem::MaybeUninit instead

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源