Rust 1.75

最後更新


Rust 1.75 發布於 2023-12-28,目前最新修補版為 1.75.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.75 最重要的改變是特徵(trait)裡終於可以直接寫 async fn 與回傳 impl Trait 的方法,不再需要額外的 crate 或手動包成 Box<dyn Future>。對新手來說,設計非同步介面時能用最直覺的寫法;首版仍有限制(官方公告另有說明),之後的版本會逐步放寬。升級時要注意:編譯器外掛(compiler plugins)支援已移除,FreeBSD 目標至少需要 12 版。

語言變更


  • 特徵可以宣告 async fn 與回傳位置 impl Trait 的方法,詳見 非同步基礎 與 特徵進階。
  • 對 usize/isize 用半開區間模式(如 0..)也能做到窮舉比對,詳見 match。
  • 保證 char 與 u32 的大小與對齊相同,詳見 bool 與 char。
  • match 可以比對部分已被移動(partially moved)的值,詳見 移動與複製。

標準庫重點


  • 新增 Option::as_slice、Option::as_mut_slice,把 Option 當成長度 0 或 1 的切片,詳見 Option。
  • 新增 File::set_modified、File::set_times 與 FileTimes,可以修改檔案時間戳,詳見 std::fs。
  • 原始指標新增 byte_add、byte_offset、byte_sub 等以位元組為單位的位移方法,詳見 std::ptr。
  • 新增 IpAddr::to_canonical、Ipv6Addr::to_canonical,詳見 std::net。
  • VecDeque<u8> 實作 BufRead,ExitCode 實作 Default。

Cargo 與工具


  • 在工作區(workspace)中新增套件時,會自動加入 [workspace.members],詳見 工作區。
  • Cargo.toml 可以不寫 version 欄位。
  • 官方發行的 rustc 套用 BOLT 與 -Ccodegen-units=1 最佳化,x86_64 Linux 上的編譯速度再提升。

相容性注意


  • 移除編譯器外掛支援。
  • FreeBSD 目標至少需要 12 版;MIPS 的 tier 2 目標降為 tier 3。
  • const 情境中的未對齊存取改為硬錯誤。

特徵中的回傳位置 impl Trait


以前特徵方法不能寫 -> impl Iterator<...>,只能回傳 Box<dyn Iterator> 或定義關聯型別。現在每個實作可以回傳自己的具體迭代器型別,呼叫端只知道「這是某種迭代器」,同樣的語法也是特徵裡 async fn 的基礎。
範例:
  1. trait Shelf {
  2. fn items(&self) -> impl Iterator<Item = &str>;
  3. }
  4. struct Bookshelf {
  5. books: Vec<String>,
  6. }
  7. impl Shelf for Bookshelf {
  8. fn items(&self) -> impl Iterator<Item = &str> {
  9. self.books.iter().map(|b| b.as_str())
  10. }
  11. }
  12. fn main() {
  13. let shelf = Bookshelf {
  14. books: vec!["Rust".to_string(), "Go".to_string(), "Zig".to_string()],
  15. };
  16. for (i, title) in shelf.items().enumerate() {
  17. println!("{}: {}", i + 1, title);
  18. }
  19. let long: Vec<&str> = shelf.items().filter(|t| t.len() > 2).collect();
  20. println!("{:?}", long);
  21. }

輸出:
  1. 1: Rust
  2. 2: Go
  3. 3: Zig
  4. ["Rust", "Zig"]

Option::as_slice


as_slice 把 Some(x) 變成只有一個元素的切片、None 變成空切片,讓接受 &[T] 的函式可以直接處理 Option;as_mut_slice 則提供可修改的版本。
範例:
  1. fn total(values: &[i32]) -> i32 {
  2. values.iter().sum()
  3. }
  4. fn main() {
  5. let some = Some(5);
  6. let none: Option<i32> = None;
  7. println!("{:?}", some.as_slice());
  8. println!("{:?}", none.as_slice());
  9. let mut extra = Some(10);
  10. if let [x] = extra.as_mut_slice() {
  11. *x += 1;
  12. }
  13. println!("{:?}", extra);
  14. let base = [1, 2, 3];
  15. let sum = total(&base) + total(some.as_slice()) + total(none.as_slice());
  16. println!("{}", sum);
  17. }

輸出:
  1. [5]
  2. []
  3. Some(11)
  4. 11

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

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

資料來源