Rust 1.29

最後更新


Rust 1.29 發布於 2018-09-13,目前最新修補版為 1.29.2,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.29 是為 1.30 與 1.31(Rust 2018 edition)鋪路的小版本,主角是兩個和警告有關的 Cargo 工具:cargo fix 會自動套用編譯器建議來修正警告,也能協助把專案從 2015 edition 遷移到 2018;cargo clippy 預覽版提供大量額外的 lint,抓出語法正確但很可能有錯的寫法。標準庫只穩定了 Iterator::flatten、Rc::downcast、Arc::downcast 三個 API。升級時要注意:std::env::home_dir 因行為不直覺而被棄用(Rust 1.87 起已取消棄用)。

標準庫重點


  • 新增 Iterator::flatten,把「迭代器的迭代器」攤平成一層,詳見 迭代器。
  • 新增 Rc::downcast、Arc::downcast,把 Rc<dyn Any> 轉回具體型別,詳見 std::any。
  • Once::call_once 不再要求 Once 是 'static,詳見 std::sync。
  • Cell<T> 允許 T 為大小不固定的型別;Box<CStr>、Box<OsStr>、Box<Path> 實作 Clone。
  • OsString 與 &str 可以直接比較是否相等,詳見 std::ffi。

Cargo 與工具


  • 新增 cargo fix,自動修正部分警告並協助遷移 edition,詳見 Edition 演進。
  • 可以用 rustup component add clippy-preview 安裝 Clippy 預覽版,執行 cargo clippy,詳見 開發工具。
  • cargo doc --document-private-items 也會產生私有項目的文件,詳見 文件註解。
  • Cargo 會自動修正被 git 合併弄壞的 lock 檔,加 --locked 可停用;cargo install 支援 --target 交叉編譯。
  • rustc 與 rustdoc 編譯失敗時結束碼為 1,panic 時為 101;rustdoc 新增 --cap-lints。
  • 升級到 LLVM 7。

相容性注意


  • std::env::home_dir 已棄用(Rust 1.87 起取消棄用),str::slice_unchecked、slice_unchecked_mut 也已棄用,改用 str::get_unchecked(begin..end),詳見 棄用 API。
  • cfg 屬性與 --cfg 命令列旗標的檢查更嚴格;rustc 不再默默忽略目標規格中的無效資料。

Iterator::flatten


當迭代器的每個元素本身也可以迭代(例如 Vec<Vec<T>>、Option<T>、split 的結果),flatten 會把它們依序串成一條。Option 也可以迭代,Some(x) 產生一個元素、None 不產生,所以 flatten 也能順便濾掉解析失敗的值。
範例:
  1. fn main() {
  2. let teams = vec![vec!["Amy", "Ben"], vec![], vec!["Cat"]];
  3. let everyone: Vec<&str> = teams.into_iter().flatten().collect();
  4. println!("{:?}", everyone);
  5. let inputs = ["3", "x", "5", ""];
  6. let numbers: Vec<i32> = inputs.iter().map(|s| s.parse().ok()).flatten().collect();
  7. println!("{:?}", numbers);
  8. let lines = ["a,b", "c", "d,e,f"];
  9. let parts: Vec<&str> = lines.iter().map(|l| l.split(',')).flatten().collect();
  10. println!("{:?}", parts);
  11. }

輸出:
  1. ["Amy", "Ben", "Cat"]
  2. [3, 5]
  3. ["a", "b", "c", "d", "e", "f"]

注意:map(...).flatten() 和早就存在的 flat_map(...) 等價,Clippy 會建議改寫成 flat_map;flatten 只攤平一層。

Rc::downcast


Rc<dyn Any> 可以裝任何 'static 型別,但用的時候需要知道實際型別。downcast::<T>() 檢查內容是不是 T:是就回傳 Ok(Rc<T>),不是就把原本的 Rc<dyn Any> 放在 Err 裡還給你,可以接著嘗試其他型別。Arc::downcast 用法相同,但要求 dyn Any + Send + Sync。
範例:
  1. use std::any::Any;
  2. use std::rc::Rc;
  3. fn describe(value: Rc<dyn Any>) -> String {
  4. match value.downcast::<String>() {
  5. Ok(s) => format!("字串:{}", s),
  6. Err(other) => match other.downcast::<i32>() {
  7. Ok(n) => format!("整數:{}", n),
  8. Err(_) => String::from("不認得的型別"),
  9. },
  10. }
  11. }
  12. fn main() {
  13. let items: Vec<Rc<dyn Any>> = vec![
  14. Rc::new(String::from("Ferris")),
  15. Rc::new(42i32),
  16. Rc::new(1.5f64),
  17. ];
  18. for item in items {
  19. println!("{}", describe(item));
  20. }
  21. }

輸出:
  1. 字串:Ferris
  2. 整數:42
  3. 不認得的型別

注意:downcast 比對的是完全相同的型別,Rc::new(42) 沒標型別時預設是 i32,用 downcast::<i64>() 會失敗;能用列舉或特徵表達的情況,通常比 Any 更好,詳見 列舉。

本版新增的棄用 API


以下 API 從 Rust 1.29 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdstr::slice_mut_uncheckeduse get_unchecked_mut(begin..end) instead
stdstr::slice_uncheckeduse get_unchecked(begin..end) instead
std::stringString::slice_mut_uncheckeduse get_unchecked_mut(begin..end) instead
std::stringString::slice_uncheckeduse get_unchecked(begin..end) instead

修補版本


版本發布日期含安全修補內容(官方原文)
1.29.12018-09-25否The standard library's str::repeat function contained an out of bounds write caused by an integer overflow. This has been fixed by deterministically panicking when an overflow happens. Thank you to Scott McMurray for responsibly disclosing this vulnerability to us.
1.29.22018-10-11否Workaround for an aliasing-related LLVM bug, which caused miscompilation.;The rls-preview component on the windows-gnu targets has been restored.

已知漏洞


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

本版已修正


編號元件摘要(官方原文)影響範圍修正於
CVE-2018-1000810stdBuffer overflow vulnerability in str::repeat()1.26.0 起,早於 1.29.11.29.1

仍影響 1.29.2


編號元件摘要(官方原文)影響範圍修正於
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
CVE-2019-1010299stdvec_deque::Iter has unsound Debug implementation1.18.0 起,早於 1.30.01.30.0

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

資料來源