Rust 1.44
最後更新
Rust 1.44 發布於 2020-06-04,目前最新修補版為
1.44.1,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.44 是一個小版本,官方公告的兩大重點是 Cargo 內建
cargo tree,以樹狀圖顯示整個相依關係,以及 async/.await 可以在 #![no_std] 環境中使用。標準庫新增 Vec<T> 從陣列建立的轉換與 PathBuf 的容量管理方法。升級時要注意:下一版(1.45)起,f64 以 as 轉成整數會改為飽和轉換,需要舊行為時改用新穩定的 to_int_unchecked。語言變更
async/.await可以在#![no_std]中使用,詳見 async 基礎。- 新增
unused_braceslint,提醒不必要的大括號。 - 被
cfg排除的外部模組(mod baz;)即使檔案不存在也能解析,語意上仍會被拒絕。
標準庫重點
Vec<T>實作From<[T; N]>(N最多 32),詳見 Vec。vec![]直接對應Vec::new(),可以用在const情境中。PathBuf新增with_capacity、capacity、clear、reserve、reserve_exact、shrink_to_fit,詳見 std::path。f32::to_int_unchecked、f64::to_int_unchecked不檢查範圍地轉成整數,詳見 f64。Layout::align_to、pad_to_align、array、extend穩定。String實作From<&mut str>;OsString實作DerefMut與IndexMut;IoSlice實作Copy;convert::Infallible實作Hash。- 整數的
from_le_bytes、to_le_bytes等位元組轉換方法改為const fn;支援 Unicode 13。
Cargo 與工具
- 新增
cargo tree顯示相依樹;cargo tree -d(--duplicates)列出同一個 crate 的多個版本,詳見 開發工具。 - rustdoc 可以用
--crate-version在側邊欄顯示版本。 - 增量編譯模式下
rustc會遵守-C codegen-units,預設使用 256 個 codegen unit。
相容性注意
mem::zeroed與mem::uninitialized用在不允許全零初始化的型別(如NonZeroU8)時改為 panic,以前只是警告。- Windows GNU 目標產生的靜態函式庫副檔名改為
.a,不再是.lib。 - 移除
rustc的-C no_integrated_as旗標;外部 LLVM 最低版本提高到 LLVM 8。
Vec::from 陣列與 const 的 vec![]
Vec::from([3, 1, 2]) 直接把陣列轉成向量,不必先轉切片再 to_vec()。String::from 也能接受 &mut str。因為空的 vec![] 會直接展開成 Vec::new(),它可以寫在 const 裡。範例:
- fn main() {
- let v: Vec<i32> = Vec::from([3, 1, 2]);
- println!("{:?}", v);
- let mut owned = String::from("abc");
- let s: String = String::from(owned.as_mut_str());
- println!("{}", s);
- const EMPTY: Vec<u8> = vec![];
- println!("{}", EMPTY.len());
- }
輸出:
- [3, 1, 2]
- abc
- 0
PathBuf 容量管理與 to_int_unchecked
PathBuf 的內部是可增長的緩衝區,現在可以像 String 一樣預先配置容量、清空後重複使用。範例後半示範 to_int_unchecked 與 1.45 起的飽和轉換:300.0 as u8 得到上限 255、-1.0 as u8 得到下限 0,不再是未定義行為。範例:
- use std::path::PathBuf;
- fn main() {
- let mut path = PathBuf::with_capacity(64);
- println!("{}", path.capacity() >= 64);
- path.push("logs");
- path.push("app.log");
- println!("{}", path.display());
- path.clear();
- println!("[{}]", path.display());
- let x = 3.9_f64;
- let n: i32 = unsafe { x.to_int_unchecked() };
- println!("{}", n);
- println!("{} {}", 300.0_f64 as u8, -1.0_f64 as u8);
- }
輸出:
- true
- logs/app.log
- []
- 3
- 255 0
注意:
to_int_unchecked 是 unsafe 方法,值是 NaN、無窮大或超出目標型別範圍時屬於未定義行為;只在已確定值落在範圍內、且需要極致效能時使用,一般轉換用 as 或 TryFrom。本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.44.1 | 2020-06-18 | 否 | rustfmt accepts rustfmt_skip in cfg_attr again.;Don't hash executable filenames on apple platforms, fixing backtraces.;Fix crashes when finding backtrace on macOS.;Clippy applies lint levels into different files. |
已知漏洞
影響
1.44.0 的已知漏洞共 18 個;在本版修正的共 0 個;仍影響最新修補版 1.44.1 的共 18 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.44.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-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-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。