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 中。
範例:
  1. fn main() {
  2. let v = vec![10, 20, 30];
  3. let arr: [i32; 3] = <[i32; 3]>::try_from(v).unwrap();
  4. println!("{:?}", arr);
  5. let wrong = vec![1, 2, 3, 4, 5];
  6. match <[i32; 3]>::try_from(wrong) {
  7. Ok(a) => println!("ok {:?}", a),
  8. Err(back) => println!("長度不符,原本的 Vec 還給你:{:?}", back),
  9. }
  10. }

輸出:
  1. [10, 20, 30]
  2. 長度不符,原本的 Vec 還給你:[1, 2, 3, 4, 5]

VecDeque::make_contiguous


VecDeque 是環狀緩衝區,資料可能分成前後兩段,所以 as_slices() 回傳兩個切片。make_contiguous() 會把資料重新排成連續的一段並回傳 &mut [T],之後就能直接用切片的排序等方法。
範例:
  1. use std::collections::VecDeque;
  2. fn main() {
  3. let mut q = VecDeque::new();
  4. q.push_back(3);
  5. q.push_back(4);
  6. q.push_front(2);
  7. q.push_front(1);
  8. let slice: &mut [i32] = q.make_contiguous();
  9. slice.sort_by(|a, b| b.cmp(a));
  10. println!("{:?}", slice);
  11. println!("{:?}", q.as_slices());
  12. }

輸出:
  1. [4, 3, 2, 1]
  2. ([4, 3, 2, 1], [])

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.48.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
CVE-2023-38497cargoCargo not respecting umask when extracting crate archives早於 1.71.11.71.1
CVE-2022-46176cargoCargo did not verify SSH host keys早於 1.66.11.66.1
CVE-2022-36113cargoExtracting malicious crates can corrupt arbitrary files早於 1.64.01.64.0
CVE-2022-36114cargoExtracting malicious crates can fill the file system早於 1.64.01.64.0
CVE-2022-21658stdRace condition in std::fs::remove_dir_all早於 1.58.11.58.1
CVE-2021-42574rustcrustc and bidirectional-override codepoints in source code早於 1.56.11.56.1
CVE-2021-31162stdDouble free in Vec::from_iter specialization when drop panics1.48.0 起,早於 1.52.01.52.0
CVE-2021-28878stdZip may call __iterator_get_unchecked twice with the same index1.14.0 起,早於 1.52.01.52.0
CVE-2021-28879stdZip can cause buffer overflow when a consumed Zip iterator is used again1.14.0 起,早於 1.52.01.52.0
CVE-2021-28876stdPanic safety issue in Zip specialization1.14.0 起,早於 1.52.01.52.0
CVE-2021-28875stdLogic bug in Read can cause buffer overflow in read_to_end()1.20.0 起,早於 1.50.01.50.0
CVE-2021-28877stdTrustedRandomAccess specialization composes incorrectly for nested iter::Zips1.11.0 起,早於 1.51.01.51.0
CVE-2020-36323stdAPI soundness issue in join() implementation of [Borrow<str>]1.28.0 起,早於 1.52.01.52.0
CVE-2020-36318stdVecDeque::make_contiguous may duplicate the contained elements1.48.0 起,早於 1.49.01.49.0
CVE-2020-36317stdString::retain allows safely creating invalid strings when abusing panic1.26.0 起,早於 1.49.01.49.0
CVE-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0

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

資料來源