Rust 1.25

最後更新


Rust 1.25 發布於 2018-03-29,目前最新修補版為 1.25.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.25 讓 use 可以巢狀分組,例如 use std::{fs::File, io::Read, path::{Path, PathBuf}};,一行就能整理一整串匯入;match 分支也允許以 | 開頭。對新手最直接的改變是 cargo new 預設建立可執行的程式(binary)而不是函式庫,建好就能 cargo run。其他重點包括升級到 LLVM 6、穩定 #[repr(align(N))] 與 ptr::NonNull,以及 Rust by Example 隨發行版一起提供。

語言變更


  • use 支援巢狀匯入群組,詳見 模組系統。
  • match 分支可以用 | 開頭,方便多行排列多個模式,詳見 match。
  • #[repr(align(N))] 穩定,可以指定型別的對齊方式,詳見 std::mem。

標準庫重點


  • 新增 ptr::NonNull<T>:保證不為空的原始指標,Option<NonNull<T>> 和 *mut T 一樣大,詳見 std::ptr。
  • Duration::new、Duration::from_secs、Duration::from_millis 可以在常數運算式中使用;Duration 移到 libcore,詳見 std::time。
  • 新增 Location::column,詳見 std::panic。
  • UnsafeCell::into_inner 改為安全函式;Unix 上的 process::Command 實作 Send。
  • Cursor<&mut Vec<u8>> 實作 Write,詳見 std::io;Component 實作 AsRef<Path>。

Cargo 與工具


  • cargo new 預設建立 binary crate,要函式庫請加 --lib,詳見 Cargo。
  • cargo new 不再自動去掉名稱中的 rust- 前綴或 -rs 後綴。
  • cargo doc 改用 cargo check 的方式產生,部分情況更快;git 相依盡可能使用硬連結,簽出更快。
  • rustdoc 預設改用 pulldown-cmark 解析 Markdown,完全遵循 CommonMark,詳見 文件註解。
  • 新增 -C lto=val 編譯選項。

相容性注意


  • 尚未穩定的 net::lookup_host 已棄用,穩定版程式改用 ToSocketAddrs,詳見 std::net。
  • 修正借用檢查器在索引運算附近錯誤允許重疊借用的問題,同時讓一些原本被誤判的正確程式碼可以編譯。
  • 移除已棄用的不穩定屬性 #[simd]。

巢狀匯入與 match 開頭的 |


同一個 crate 底下的多個路徑,現在可以用大括號層層分組,一個 use 就寫完。match 分支開頭也可以多寫一個 |,多個模式垂直排列時每行看起來一致。
範例:
  1. use std::{
  2. collections::{btree_map::Entry, BTreeMap},
  3. fmt::Write,
  4. };
  5. enum Level {
  6. Debug,
  7. Info,
  8. Warn,
  9. Error,
  10. }
  11. fn label(level: &Level) -> &'static str {
  12. match level {
  13. | Level::Debug
  14. | Level::Info => "一般",
  15. | Level::Warn
  16. | Level::Error => "需要注意",
  17. }
  18. }
  19. fn main() {
  20. let mut counts = BTreeMap::new();
  21. for level in [Level::Info, Level::Warn, Level::Debug, Level::Error, Level::Info] {
  22. match counts.entry(label(&level)) {
  23. Entry::Occupied(mut e) => *e.get_mut() += 1,
  24. Entry::Vacant(e) => {
  25. e.insert(1);
  26. }
  27. }
  28. }
  29. let mut report = String::new();
  30. write!(report, "{:?}", counts).unwrap();
  31. println!("{}", report);
  32. }

輸出:
  1. {"一般": 3, "需要注意": 2}

注意:use std::fmt::Write 匯入的是特徵,write! 寫進 String 需要它在作用域內;匯入特徵後才能呼叫該特徵的方法。

#[repr(align(N))]


對齊(alignment)決定一個值可以放在哪些記憶體位址:對齊 16 表示位址必須是 16 的倍數。#[repr(align(N))] 讓你把型別的對齊提高到 N(必須是 2 的次方),常見用途是讓資料剛好占一條 CPU 快取線(cache line),避免多執行緒互相干擾,或符合 SIMD 與硬體介面的要求。型別大小會被補齊到對齊的倍數。
以下數值為 64 位元平台的結果。
範例:
  1. use std::mem::{align_of, size_of};
  2. #[allow(dead_code)]
  3. struct Plain(i32);
  4. #[allow(dead_code)]
  5. #[repr(align(16))]
  6. struct Aligned16(i32);
  7. #[allow(dead_code)]
  8. #[repr(align(64))]
  9. struct CacheLine {
  10. counter: u64,
  11. }
  12. fn main() {
  13. println!("Plain:align {},size {}", align_of::<Plain>(), size_of::<Plain>());
  14. println!("Aligned16:align {},size {}", align_of::<Aligned16>(), size_of::<Aligned16>());
  15. println!("CacheLine:align {},size {}", align_of::<CacheLine>(), size_of::<CacheLine>());
  16. let items = [Aligned16(1), Aligned16(2)];
  17. let a = &items[0] as *const Aligned16 as usize;
  18. let b = &items[1] as *const Aligned16 as usize;
  19. println!("相鄰元素相距 {} bytes,位址是 16 的倍數:{}", b - a, a % 16 == 0);
  20. }

輸出:
  1. Plain:align 4,size 4
  2. Aligned16:align 16,size 16
  3. CacheLine:align 64,size 64
  4. 相鄰元素相距 16 bytes,位址是 16 的倍數:true

注意:align 只能提高對齊,不能降低;N 不是 2 的次方會編譯失敗。要降低對齊請用 #[repr(packed)],但存取其欄位需要特別小心。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源