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 的基礎。範例:
- trait Shelf {
- fn items(&self) -> impl Iterator<Item = &str>;
- }
- struct Bookshelf {
- books: Vec<String>,
- }
- impl Shelf for Bookshelf {
- fn items(&self) -> impl Iterator<Item = &str> {
- self.books.iter().map(|b| b.as_str())
- }
- }
- fn main() {
- let shelf = Bookshelf {
- books: vec!["Rust".to_string(), "Go".to_string(), "Zig".to_string()],
- };
- for (i, title) in shelf.items().enumerate() {
- println!("{}: {}", i + 1, title);
- }
- let long: Vec<&str> = shelf.items().filter(|t| t.len() > 2).collect();
- println!("{:?}", long);
- }
輸出:
- 1: Rust
- 2: Go
- 3: Zig
- ["Rust", "Zig"]
Option::as_slice
as_slice 把 Some(x) 變成只有一個元素的切片、None 變成空切片,讓接受 &[T] 的函式可以直接處理 Option;as_mut_slice 則提供可修改的版本。範例:
- fn total(values: &[i32]) -> i32 {
- values.iter().sum()
- }
- fn main() {
- let some = Some(5);
- let none: Option<i32> = None;
- println!("{:?}", some.as_slice());
- println!("{:?}", none.as_slice());
- let mut extra = Some(10);
- if let [x] = extra.as_mut_slice() {
- *x += 1;
- }
- println!("{:?}", extra);
- let base = [1, 2, 3];
- let sum = total(&base) + total(some.as_slice()) + total(none.as_slice());
- println!("{}", sum);
- }
輸出:
- [5]
- []
- Some(11)
- 11
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.75.0 的已知漏洞共 4 個;在本版修正的共 0 個;仍影響最新修補版 1.75.0 的共 4 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.75.0
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| CVE-2026-5222 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
| CVE-2024-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。