Rust 1.57
最後更新
Rust 1.57 發布於 2021-12-02,目前最新修補版為
1.57.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.57 讓
panic! 可以在 const 情境中使用,搭配 assert! 就能寫出編譯期檢查;Cargo 支援自訂名稱的 profile;Vec、String、HashMap 等集合新增可失敗的配置 API try_reserve。對新手來說,編譯期斷言能把錯誤提早到 cargo build 時發現。升級時要注意:衍生(derive)的 Clone 與 Debug 實作不再算是「使用」欄位,開了 #![deny(dead_code)] 的專案可能編譯失敗。語言變更
const情境中可以呼叫panic!與assert!,詳見 常數。- 巨集屬性可以寫在
#[derive]之後,並看到cfg處理前的原始輸入,詳見 程序式巨集。 - 大括號形式的巨集呼叫可以出現在運算式中,例如
m!{ .. }.method()與m!{ .. }?。 - 死碼分析忽略衍生的
Clone與Debug實作。
標準庫重點
Vec、String、VecDeque新增try_reserve、try_reserve_exact,HashMap、HashSet新增try_reserve,錯誤型別為collections::TryReserveError,詳見 集合。- 新增
Iterator::map_while,詳見 std::iter。 - 新增
[T; N]::as_slice、[T; N]::as_mut_slice,詳見 陣列。 - 新增
Command::get_program、get_args、get_envs、get_current_dir,詳見 std::process。 - 更多函式標上
#[must_use],忽略回傳值時會警告;Unicode 更新到 14.0。
Cargo 與工具
- 支援自訂 profile,例如
[profile.production]搭配inherits = "release",用--profile production建置,輸出到target/production,詳見 Cargo。
相容性注意
- 死碼分析忽略衍生的
Clone與Debug,設定#![deny(dead_code)]的專案可能因此編譯失敗。
const 中的 panic! 與編譯期斷言
const fn 裡可以呼叫 panic!,在 const 項目中用 assert! 檢查條件。如果常數求值時真的觸發 panic,就是編譯錯誤,程式根本不會產出。範例:
- const fn checked_percent(value: u32) -> u32 {
- if value > 100 {
- panic!("percent must be <= 100");
- }
- value
- }
- const HALF: u32 = checked_percent(50);
- const _: () = assert!(std::mem::size_of::<u64>() == 8);
- fn main() {
- println!("{HALF}");
- }
輸出:
- 50
把常數改成
checked_percent(150) 時,這段無法編譯:- const TOO_MUCH: u32 = checked_percent(150);
- const fn checked_percent(value: u32) -> u32 {
- if value > 100 {
- panic!("percent must be <= 100");
- }
- value
- }
- fn main() {
- println!("{TOO_MUCH}");
- }
輸出:
- error[E0080]: evaluation panicked: percent must be <= 100
- --> main.rs:1:23
- |
- 1 | const TOO_MUCH: u32 = checked_percent(150);
- | ^^^^^^^^^^^^^^^^^^^^ evaluation of `TOO_MUCH` failed inside this call
注意:這版的
const 中 panic! 只接受靜態字串,或單一個以 {} 插入的 &str 值,還不支援完整的格式化功能。try_reserve
一般的
reserve 在配置記憶體失敗時會中止整個程式;try_reserve 改為回傳 Result,失敗時得到 TryReserveError,集合本身仍然可用。範例:
- fn main() {
- let mut buf: Vec<u8> = Vec::new();
- match buf.try_reserve(1024) {
- Ok(()) => println!("reserved: {}", buf.capacity() >= 1024),
- Err(e) => println!("failed: {e}"),
- }
- let mut s = String::new();
- match s.try_reserve(usize::MAX) {
- Ok(()) => println!("unexpected"),
- Err(e) => println!("failed: {e}"),
- }
- s.push_str("still usable");
- println!("{s}");
- }
輸出:
- reserved: true
- failed: memory allocation failed because the computed capacity exceeded the collection's maximum
- still usable
注意:
try_reserve 回傳成功不代表作業系統真的給了記憶體,例如 Linux 開啟 overcommit 時,實際使用時仍可能拿不到。Iterator::map_while
map_while(f) 對每個元素呼叫 f,只要回傳 Some 就繼續並取出內容,第一次回傳 None 就停止。下面在遇到第一個無法解析的字串時結束,後面的 "15" 不會被處理。範例:
- fn main() {
- let input = ["4", "8", "x", "15"];
- let numbers: Vec<i32> = input.iter().map_while(|s| s.parse().ok()).collect();
- println!("{:?}", numbers);
- }
輸出:
- [4, 8]
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.57.0 的已知漏洞共 9 個;在本版修正的共 0 個;仍影響最新修補版 1.57.0 的共 9 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.57.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 |
| CVE-2023-38497 | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | 1.71.1 |
| CVE-2022-46176 | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | 1.66.1 |
| CVE-2022-36113 | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | 1.64.0 |
| CVE-2022-36114 | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | 1.64.0 |
| CVE-2022-21658 | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | 1.58.1 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。