Rust 1.52
最後更新
Rust 1.52 發布於 2021-05-06,目前最新修補版為
1.52.1,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.52 最大的改變在工具:先跑
cargo check 再跑 cargo clippy 時,Clippy 以前會因為 Cargo 的建置快取而沒有真的執行,現在不論順序都會得到預期結果。標準庫新增常用的 str::split_once、slice::partition_point,以及一批 char 的關聯函式與常數。升級時要注意建置腳本不能再設定 RUSTC_BOOTSTRAP,並請直接使用本版的最新修補版(見下方「修補版本」)。語言變更
- 新增
unsafe_op_in_unsafe_fnlint,檢查unsafe fn內的不安全操作是否包在unsafe區塊裡;預設為 allow,詳見 unsafe。 - 陣列的可變參考可以轉型成指向元素型別的原始指標。
標準庫重點
- 新增
str::split_once、str::rsplit_once,在第一個(最後一個)分隔符處把字串切成兩半,詳見 str。 - 新增
slice::partition_point,在已排序切片中用二分搜尋找分界點,詳見 切片。 - 新增
char::from_u32、char::from_digit、char::from_u32_unchecked、char::decode_utf16與常數char::MAX、char::REPLACEMENT_CHARACTER、char::UNICODE_VERSION,詳見 char。 - 新增
Arguments::as_str,詳見 std::fmt。 - 所有整數除法與取餘數運算都是
const;char::len_utf8、to_ascii_uppercase等方法成為const fn。 OsString實作Extend與FromIterator;Arc<impl Error>實作Error,詳見 std::error。
Cargo 與工具
cargo check與cargo clippy的快取不再互相干擾,詳見 開發工具。cargo test -- foo bar可以傳入多個測試過濾條件,詳見 單元測試。- rustdoc 的 lint 改為工具 lint,名稱加上
rustdoc::前綴;文件 Markdown 支援工作清單- [x]。 - 預設 LLVM 升級到 12。
相容性注意
- 建置腳本不能再設定
RUSTC_BOOTSTRAP。 - 移除
x86_64-rumprun-netbsd目標;x86_64-sun-solaris改用x86_64-pc-solaris。 - rustdoc 指定程式碼區塊語言時只接受
,、空格與 tab 作為分隔符。 - 舊版
proc-macro-hack可能因空白處理改變而 panic,執行cargo update即可修正。
str::split_once
解析
key=value 這類字串時,split_once('=') 回傳 Option<(&str, &str)>:找到分隔符就切成前後兩段,找不到就是 None。rsplit_once 從右邊找。範例:
- fn main() {
- let line = "name=Rust=lang";
- println!("{:?}", line.split_once('='));
- println!("{:?}", line.rsplit_once('='));
- println!("{:?}", "no-equals".split_once('='));
- if let Some((key, value)) = "port: 8080".split_once(": ") {
- println!("{key} -> {value}");
- }
- }
輸出:
- Some(("name", "Rust=lang"))
- Some(("name=Rust", "lang"))
- None
- port -> 8080
slice::partition_point
對一個「前段都符合條件、後段都不符合」的切片,
partition_point(條件) 用二分搜尋回傳第一個不符合條件的索引。常用在已排序的 Vec 中找插入位置,讓插入後仍保持排序。範例:
- fn main() {
- let sorted = [1, 3, 5, 7, 9, 11];
- let idx = sorted.partition_point(|&x| x < 6);
- println!("{idx}");
- let mut v = vec![10, 20, 40, 50];
- let pos = v.partition_point(|&x| x < 30);
- v.insert(pos, 30);
- println!("{:?}", v);
- }
輸出:
- 3
- [10, 20, 30, 40, 50]
char 的關聯函式與常數
char::from_digit(數字, 進位) 把數字轉成對應的字元;char::from_u32 把 Unicode 碼位轉成 char,遇到不是合法字元的值(如代理區 0xD800)回傳 None。char::MAX 是最大的合法碼位,REPLACEMENT_CHARACTER 是解碼失敗時常用的替代字元 U+FFFD。範例:
- fn main() {
- println!("{:?}", char::from_digit(7, 10));
- println!("{:?}", char::from_digit(11, 16));
- println!("{:?}", char::from_u32(0x4E2D));
- println!("{:?}", char::from_u32(0xD800));
- println!("{:?}", char::MAX);
- println!("{}", char::REPLACEMENT_CHARACTER);
- }
輸出:
- Some('7')
- Some('b')
- Some('中')
- None
- '\u{10ffff}'
- �
本版新增的棄用 API
以下 API 從 Rust 1.52 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std::alloc | LayoutErr | Name does not follow std convention, use LayoutError |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.52.1 | 2021-05-10 | 否 | - |
已知漏洞
影響
1.52.0 的已知漏洞共 11 個;在本版修正的共 0 個;仍影響最新修補版 1.52.1 的共 11 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.52.1
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| 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 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。