Rust 1.38

最後更新


Rust 1.38 發布於 2019-09-26,目前最新修補版為 1.38.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.38 的重點是管線化編譯(pipelined compilation):相依的 crate 只要完成型別資訊與 metadata,下游 crate 就能開始編譯,不必等上游的程式碼產生結束,多 crate 專案的建置時間在部分情況下明顯縮短,而且不需要任何設定。同一版也新增了 std::any::type_name,並讓 rustc 對已知會造成未定義行為的 mem::uninitialized、mem::zeroed 用法發出警告。

語言變更


  • #[global_allocator] 可以用在子模組中。
  • #[deprecated] 可以標在巨集上。

標準庫重點


  • any::type_name 取得型別名稱的字串,主要用於除錯,詳見 std::any。
  • Duration 新增 as_secs_f32、as_secs_f64、from_secs_f32、from_secs_f64、mul_f32、mul_f64、div_f32、div_f64,以浮點數表示或運算時間,詳見 std::time。
  • 所有數值型別新增歐幾里得除法 div_euclid 與餘數 rem_euclid,整數另有 checked、overflowing、wrapping 版本,詳見 數值。
  • 原始指標新增 <*const T>::cast 與 <*mut T>::cast,詳見 std::ptr。
  • Clone、Debug 等 prelude trait 的 derive 巨集可以用和 trait 相同的路徑取得,例如 std::clone::Clone;內建巨集也都能從 std 根取得,例如 std::include_bytes!。
  • Arc<[T]> 與 Rc<[T]> 實作 FromIterator<T>;iter::StepBy、Peekable、Take 實作 DoubleEndedIterator。
  • slice::concat、join 可以接受 &[T] 作為分隔或元素。

Cargo 與工具


  • Cargo 支援管線化編譯。
  • --features 可以重複傳入多次來啟用多個 feature。
  • rustdoc:pub use 上的文件會加在被重新匯出項目的文件前面。

相容性注意


  • rustc 1.38.0 無法建置 x86_64-unknown-uefi 平台;armv7-unknown-linux-gnueabihf 平台與 libc 等部分 crate 已知有問題。

std::any::type_name


type_name::<T>() 回傳型別名稱的字串,搭配泛型可以看出編譯器推斷出的實際型別,對理解泛型與型別推斷很有幫助。官方文件說明輸出格式不保證穩定,只能用於除錯,不要拿來比對或當作識別碼。範例的 Vec<u8> 印成 alloc::vec::Vec<u8>,顯示出它實際定義在 alloc crate。
範例:
  1. use std::any::type_name;
  2. fn name_of<T>(_: &T) -> &'static str {
  3. type_name::<T>()
  4. }
  5. fn main() {
  6. println!("{}", type_name::<i32>());
  7. println!("{}", name_of(&vec![1u8]));
  8. println!("{}", name_of(&"hi"));
  9. }

輸出:
  1. i32
  2. alloc::vec::Vec<u8>
  3. &str

Duration 的浮點運算與歐幾里得除法


Duration::from_secs_f64(1.5) 直接用秒數建立時間長度,mul_f64、div_f32 讓「乘以倍率」不必先換算成奈秒。歐幾里得除法則處理負數:一般的 % 結果和被除數同號,-7 % 3 是 -1;rem_euclid 永遠回傳非負數,-7 對 3 取餘得 2,計算循環索引時特別好用。
範例:
  1. use std::time::Duration;
  2. fn main() {
  3. let d = Duration::from_secs_f64(1.5);
  4. println!("{:?}", d);
  5. println!("{}", d.as_secs_f64());
  6. println!("{:?}", d.mul_f64(2.0));
  7. println!("{:?}", d.div_f32(4.0));
  8. println!("{} {}", -7 % 3, (-7i32).rem_euclid(3));
  9. println!("{} {}", -7 / 3, (-7i32).div_euclid(3));
  10. }

輸出:
  1. 1.5s
  2. 1.5
  3. 3s
  4. 375ms
  5. -1 2
  6. -2 -3

注意:Duration::from_secs_f64 傳入負數、NaN 或超出範圍的值會 panic。

本版新增的棄用 API


以下 API 從 Rust 1.38 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
std::syncONCE_INITthe Once::new() function is now preferred

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.38.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-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-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。

資料來源