Rust 1.22

最後更新


Rust 1.22 發布於 2017-11-22,目前最新修補版為 1.22.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.22 的重點是 ? 運算子可以用在 Option 上:值是 None 就直接從函式回傳 None,一連串「可能沒有值」的步驟不必再層層 match。另外 x += &8 這種「數值加上參考」的寫法也能編譯了,不用再手動解參考。升級時要注意:這版還不能在同一個函式裡讓 ? 混用 Result 與 Option;而 T op= &T 的新規則會讓少數原本推論得出來的型別變得無法推論。

語言變更


  • Option<T> 可以使用 ? 運算子,詳見 Option。
  • 數值型別支援 T op= &T,例如 let mut x = 2; x += &8;,詳見 std::ops。
  • 實作 Drop 的型別可以放進 const 與 static,詳見 常數與靜態變數。
  • Unicode 跳脫序列裡可以使用底線;non_snake_case lint 不再檢查 extern 的 no_mangle 函式。

標準庫重點


  • Box<Error> 實作 From<Cow<str>>,詳見 std::error。
  • mem::Discriminant 保證是 Send + Sync。
  • Instant += Duration 會正確偵測溢位,詳見 std::time。
  • &mut Hasher 與 Box<Hasher> 實作 Hasher,詳見 std::hash;SplitWhitespace 實作 Debug。

Cargo 與工具


  • examples/ 下含 main.rs 的子目錄會建置成多檔案範例,詳見 Cargo。
  • Cargo.lock 的 [root] 改為 [package],舊格式仍可用,執行 cargo update 會更新。
  • 支援把 git 來源的相依 vendor 到本地。
  • 文件測試的程式碼區塊可以標記 compile_fail,表示這段應該編譯失敗,詳見 文件測試。
  • debug 建置預設使用 16 個 codegen unit 且不跨單元內聯,加快編譯;macOS 上的回溯更完整。

相容性注意


  • T op= &T 讓部分型別推論失敗。
  • rustc 可建置的最低 Android 版本由 2.3 提高到 4.0;移除 PNaCl 目標 le32-unknown-nacl。

Option 的 ? 運算子


? 寫在 Option 後面時:是 Some(v) 就取出 v 繼續執行;是 None 就讓整個函式立刻回傳 None。所以使用 ? 的函式本身也要回傳 Option。需要把 Result 接進來時,先用 .ok() 把它轉成 Option。
範例:
  1. fn port_of(line: &str) -> Option<u16> {
  2. let pos = line.find(':')?;
  3. let digits = line.get(pos + 1..)?;
  4. digits.trim().parse().ok()
  5. }
  6. fn first_char_upper(words: &[&str]) -> Option<char> {
  7. let first = words.first()?;
  8. let c = first.chars().next()?;
  9. Some(c.to_ascii_uppercase())
  10. }
  11. fn main() {
  12. println!("{:?}", port_of("localhost: 8080"));
  13. println!("{:?}", port_of("localhost"));
  14. println!("{:?}", port_of("localhost:abc"));
  15. println!("{:?} {:?}", first_char_upper(&["rust", "go"]), first_char_upper(&[]));
  16. }

輸出:
  1. Some(8080)
  2. None
  3. None
  4. Some('R') None

注意:在回傳 Result 的函式裡對 Option 用 ? 會編譯失敗,要先用 ok_or 轉成 Result,詳見 錯誤處理實務。

數值的 T op= &T


走訪 &Vec 或 .iter() 時拿到的是參考 &i32。以前累加要寫成 total += *p,現在 +=、-= 等複合指定運算子可以直接接受參考,標準庫為所有數值型別補上了這些實作。
範例:
  1. fn main() {
  2. let prices = vec![120, 45, 300];
  3. let mut total = 0;
  4. for p in &prices {
  5. total += p;
  6. }
  7. println!("總計:{}", total);
  8. let mut budget = 1000.0;
  9. let costs = [250.5, 99.5];
  10. for c in costs.iter() {
  11. budget -= c;
  12. }
  13. println!("剩餘:{}", budget);
  14. }

輸出:
  1. 總計:465
  2. 剩餘:650

注意:這只適用於標準庫的數值型別;自己定義的型別要支援 += &T,需要自行為 &T 實作 AddAssign。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
1.22.12017-11-22否Update Cargo to fix an issue with macOS 10.13 "High Sierra"

已知漏洞


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

仍影響 1.22.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-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0
CVE-2019-16760cargoCargo prior to Rust 1.26.0 may download the wrong dependency早於 1.26.01.26.0
CVE-2019-1010299stdvec_deque::Iter has unsound Debug implementation1.18.0 起,早於 1.30.01.30.0
CVE-2018-1000622rustdocUncontrolled search path element vulnerability in rustdoc plugins早於 1.27.11.27.1
CVE-2018-25008stdInsufficient synchronization in Arc::get_mut1.3.0 起,早於 1.29.01.29.0

本版已停止支援,不會再修正這些漏洞;請升級到 1.100.1。

資料來源