Rust 1.14

最後更新


Rust 1.14 發布於 2016-12-22,目前最新修補版為 1.14.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.14 最大的消息不在語言本身:rustup 發布 1.0,成為官方建議的安裝方式,可以輕鬆切換 stable、beta、nightly 並加裝交叉編譯用的標準庫。這版也以實驗方式支援 WebAssembly 目標 wasm32-unknown-emscripten,並新增 MIPS、PowerPC、s390x 等主機平台。語言方面,.. 可以在元組與元組結構的模式中略過任意數量的欄位。升級要注意:多個前向相容 lint 改為預設拒絕(deny),以前只是警告的程式碼現在會編譯失敗;rustup 預設不再安裝文件,需要時執行 rustup component add rust-docs。

語言變更


  • .. 可以在列舉變體、結構與元組的模式中比對多個元組欄位(RFC 1492),詳見 模式。
  • 安全的 fn 項目可以轉型成 unsafe fn 指標,詳見 unsafe。
  • use * 與 use ::* 都從 crate 根部做 glob 匯入,詳見 模組系統。
  • Vec<Box<Fn()>> 裡的閉包不必明確解參考就能呼叫,詳見 閉包。

標準庫重點


  • println!() 不帶任何引數時印出一個換行,詳見 std::fmt。
  • String 實作 From<Cow<str>>、Vec<T> 實作 From<Cow<[T]>>;Cow<str> 可以用 + 接上 &str 或 Cow<str>,詳見 std::borrow。
  • Wrapping 實作標準的二元與一元運算子,以及 Sum、Product,詳見 std::num。
  • io::Error 實作 From<io::ErrorKind>,詳見 std::io。
  • HashMap 的內部記憶體配置對快取更友善,部分操作明顯加快,也不再重複使用隨機種子,詳見 HashMap。
  • 原子型別實作 RefUnwindSafe;ReadDir 實作 Debug;Vec::extend 遇到切片時特化成 extend_from_slice。

Cargo 與工具


  • rustup 是官方建議的安裝方式,詳見 安裝。
  • 實驗性支援 WebAssembly:rustup target add wasm32-unknown-emscripten 後即可用 rustc --target=wasm32-unknown-emscripten 編譯,已知仍有重大缺陷。
  • 新增 MIPS、PowerPC、s390x 的 Linux 主機建置,以及 ARM MUSL 的標準庫建置,都是 tier 2 平台。
  • Cargo 把 rustc --print cfg 的值傳給建置腳本;CARGO_HOME 唯讀時也能運作;測試與 bench 設定檔忽略 panic 設定,避免 panic=abort 讓單一測試失敗中止整個測試套件,詳見 Cargo。
  • Rust 發行版啟用行號除錯資訊;rustc --version --verbose 會顯示 LLVM 版本。

相容性注意


  • 多個前向相容 lint 改為預設拒絕,例如浮點數常數不能用在模式中、結構或列舉常數必須 #[derive(PartialEq, Eq)] 才能用於模式。
  • 原子型別的 Ordering 列舉不能再窮舉比對,詳見 std::sync::atomic。
  • 沒有函式本體的特徵方法不能在參數使用模式;列舉不能是不定大小型別。
  • 實作比特徵本身要求更多生命週期條件的 impl 會被拒絕。
  • $crate 巨集變數可使用的位置變少,詳見 宣告式巨集。

模式中的 .. 略過部分欄位


以前元組結構只能用 Point(..) 一次略過全部欄位,想取第一個欄位就得把其他欄位都寫成 _。1.14 起 .. 可以放在開頭、中間或結尾,只取需要的欄位,寫法和具名欄位結構的 Point { x, .. } 一致。
範例:
  1. struct Rgb(u8, u8, u8);
  2. enum Shape {
  3. Rect(u32, u32, &'static str),
  4. }
  5. fn main() {
  6. let c = Rgb(255, 128, 0);
  7. let Rgb(red, ..) = c;
  8. let Rgb(_, green, ..) = c;
  9. let Rgb(.., blue) = c;
  10. println!("red = {}, green = {}, blue = {}", red, green, blue);
  11. let t = (1, 2, 3, 4, 5);
  12. let (first, .., last) = t;
  13. println!("{} {}", first, last);
  14. let s = Shape::Rect(3, 4, "藍");
  15. match s {
  16. Shape::Rect(w, h, ..) => println!("面積 {}", w * h),
  17. }
  18. let Shape::Rect(.., color) = s;
  19. println!("顏色 {}", color);
  20. }

輸出:
  1. red = 255, green = 128, blue = 0
  2. 1 5
  3. 面積 12
  4. 顏色 藍

注意:一個模式裡只能出現一次 ..,寫成 (a, .., b, .., c) 會因為無法判斷略過哪些欄位而編譯失敗。

println!() 與 Cow 轉換


println!() 不帶引數就印出空行,不必再寫 println!("")。Cow<str> 表示「可能借用、也可能擁有」的字串;1.14 起它能直接用 into() 或 String::from 轉成 String,也能用 + 接上字串切片。
範例:
  1. use std::borrow::Cow;
  2. fn normalize(input: &str) -> Cow<'_, str> {
  3. if input.contains(' ') {
  4. Cow::Owned(input.replace(' ', "_"))
  5. } else {
  6. Cow::Borrowed(input)
  7. }
  8. }
  9. fn main() {
  10. println!("第一行");
  11. println!();
  12. println!("空一行之後");
  13. let a: String = normalize("hello world").into();
  14. let b: String = String::from(normalize("rust"));
  15. println!("{} {}", a, b);
  16. let tail = Cow::Borrowed("abc") + "def";
  17. println!("{}", tail);
  18. }

輸出:
  1. 第一行
  2. 空一行之後
  3. hello_world rust
  4. abcdef

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.14.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-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-2018-1000657stdBuffer overflow vulnerability in VecDeque::reserve()1.3.0 起,早於 1.22.01.22.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
CVE-2017-20004stdMutexGuard<Cell<i32>> must not be Sync早於 1.19.01.19.0

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

資料來源