Rust 1.48
最後更新
Rust 1.48 發布於 2020-11-19,目前最新修補版為
1.48.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.48 的主角是文件工具 rustdoc:文件註解裡可以直接用項目路徑建立連結(intra-doc links),也能用
#[doc(alias)] 加入搜尋別名。標準庫方面,任意長度的陣列都實作了 TryFrom<Vec<T>>,可以把長度剛好的 Vec 轉成固定長度陣列。對新手來說,寫文件互相連結不必再手寫相對網址;升級時要注意 const fn 中參考提升為 'static 的規則變得和一般函式一致。語言變更
unsafe關鍵字在語法上可以寫在模組前面;語意上仍會被拒絕,但程序式巨集可以解析它。
標準庫重點
- 任意長度的陣列實作
TryFrom<Vec<T>>,詳見 型別轉換 與 陣列。 - 新增
VecDeque::make_contiguous,詳見 集合。 - 新增
future::ready、future::pending,詳見 std::future。 - 新增
slice::as_ptr_range、slice::as_mut_ptr_range,詳見 切片。 Option::is_some、Option::as_ref、Result::is_ok、Ordering::reverse等方法成為const fn。matches!巨集允許結尾逗號;Vec<A>實作PartialEq<[B]>。
Cargo 與工具
- rustdoc 支援 intra-doc links,例如
/// [`crate::foo::Foo`]會自動連到該型別,詳見 註解與文件。 - 項目可以加上
#[doc(alias = "...")],在 rustdoc 搜尋時用別名找到。 - 穩定
-C link-self-contained=<yes|no>;linux-gnu目標可以使用-C target-feature=+crt-static。
相容性注意
const fn中的參考提升為'static的規則改成和一般函式相同,&foo()在const fn中不再被提升。mem::uninitialized用在不允許全零初始化的型別上會 panic。#[link_name]、#[cold]等屬性放在沒有作用的位置會產生警告;#[target_feature]放錯位置會編譯錯誤。- 外部例外被
catch_unwind捕捉時會中止程式。
從 Vec 轉成固定長度陣列
<[T; N]>::try_from(vec) 在長度剛好等於 N 時成功,得到陣列;長度不符時回傳 Err,裡面就是原本的 Vec,資料不會遺失。範例直接使用 try_from:1.48 當時需要先 use std::convert::TryFrom,2021 版次(edition)起 TryFrom 已在 prelude 中。範例:
- fn main() {
- let v = vec![10, 20, 30];
- let arr: [i32; 3] = <[i32; 3]>::try_from(v).unwrap();
- println!("{:?}", arr);
- let wrong = vec![1, 2, 3, 4, 5];
- match <[i32; 3]>::try_from(wrong) {
- Ok(a) => println!("ok {:?}", a),
- Err(back) => println!("長度不符,原本的 Vec 還給你:{:?}", back),
- }
- }
輸出:
- [10, 20, 30]
- 長度不符,原本的 Vec 還給你:[1, 2, 3, 4, 5]
VecDeque::make_contiguous
VecDeque 是環狀緩衝區,資料可能分成前後兩段,所以 as_slices() 回傳兩個切片。make_contiguous() 會把資料重新排成連續的一段並回傳 &mut [T],之後就能直接用切片的排序等方法。範例:
- use std::collections::VecDeque;
- fn main() {
- let mut q = VecDeque::new();
- q.push_back(3);
- q.push_back(4);
- q.push_front(2);
- q.push_front(1);
- let slice: &mut [i32] = q.make_contiguous();
- slice.sort_by(|a, b| b.cmp(a));
- println!("{:?}", slice);
- println!("{:?}", q.as_slices());
- }
輸出:
- [4, 3, 2, 1]
- ([4, 3, 2, 1], [])
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.48.0 的已知漏洞共 20 個;在本版修正的共 0 個;仍影響最新修補版 1.48.0 的共 20 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.48.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 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-31162 | std | Double free in Vec::from_iter specialization when drop panics | 1.48.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28878 | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28879 | std | Zip can cause buffer overflow when a consumed Zip iterator is used again | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28876 | std | Panic safety issue in Zip specialization | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2021-28875 | std | Logic bug in Read can cause buffer overflow in read_to_end() | 1.20.0 起,早於 1.50.0 | 1.50.0 |
| CVE-2021-28877 | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起,早於 1.51.0 | 1.51.0 |
| CVE-2020-36323 | std | API soundness issue in join() implementation of [Borrow<str>] | 1.28.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2020-36318 | std | VecDeque::make_contiguous may duplicate the contained elements | 1.48.0 起,早於 1.49.0 | 1.49.0 |
| CVE-2020-36317 | std | String::retain allows safely creating invalid strings when abusing panic | 1.26.0 起,早於 1.49.0 | 1.49.0 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。