Rust 1.47
最後更新
Rust 1.47 發布於 2020-10-08,目前最新修補版為
1.47.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.47 沒有新的語言功能,最受期待的改變在標準庫:
Debug、PartialEq 等特徵(trait)不再只實作到長度 32 的陣列,任何長度的陣列都能直接印出與比較。這是標準庫內部先用上 const generics 的成果,對新手來說,[0; 34] 印不出來這種奇怪的限制從此消失。同一版的 panic 回溯(backtrace)變得精簡,並升級到 LLVM 11。語言變更
- 沒有使用的閉包(closure)會產生警告,詳見 閉包。
標準庫重點
std/core中的特徵對任意長度的陣列都有實作,不再限於長度小於 33,詳見 陣列。- 新增
Result::as_deref、Result::as_deref_mut,詳見 Result。 - 新增
Range::is_empty、RangeInclusive::is_empty,詳見 運算子特徵。 - 新增
Vec::leak、pointer::offset_from,以及常數f32::TAU、f64::TAU(2π),詳見 Vec 與 f64。 - 整數的
checked_*、saturating_*與char/u8的is_ascii_*等方法可以在const中使用。 ops::RangeFull與ops::Range實作Default;panic::Location實作Copy、Eq、Hash、Ord等特徵。
Cargo 與工具
build-dependencies預設以opt-level0 編譯,可用[profile.release.build-override]覆寫,詳見 Cargo。cargo help會顯示指令的 man page;workspace.default-members會遵守workspace.exclude。RUST_BACKTRACE的輸出更精簡,只留下與錯誤相關的幾層;需要完整內容時設RUST_BACKTRACE=full,詳見 backtrace。- 新增
-C control-flow-guard,在 Windows 開啟 Control Flow Guard;rustdoc 新增 Ayu 主題。
相容性注意
linux-gnu目標至少需要核心 2.6.32 與 glibc 2.11。- 最低支援的 Emscripten 版本提高到 1.39.20;CloudABI 目標降為 tier 3。
macro_rules!中程序式巨集的展開方式改變,不衛生(unhygienic)的巨集可能出現編譯錯誤。
任意長度陣列的特徵實作
陣列型別
[T; N] 把長度 N 寫在型別裡。以前標準庫只能為 0 到 32 每種長度各寫一份實作,長度 33 以上的陣列連 {:?} 都印不出來。現在 Debug、PartialEq、Clone 等特徵對任何長度都成立。範例:
- fn main() {
- let big = [7u8; 40];
- let copy = big;
- println!("{}", big == copy);
- println!("{:?}", &big[..5]);
- let zeros = [0u16; 34];
- let total: u16 = zeros.iter().sum();
- println!("{} {:?}", total, zeros == [0; 34]);
- println!("{:?}", [1; 33]);
- }
輸出:
- true
- [7, 7, 7, 7, 7]
- 0 true
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Result::as_deref 與 Range::is_empty
Result<String, E> 呼叫 as_deref() 會得到 Result<&str, &E>,不必移動原本的值就能用 &str 處理成功的結果。is_empty() 則回答「這個範圍有沒有任何元素」,例如 3..3 與 1..=0 都是空的。範例:
- fn find_user(id: u32) -> Result<String, String> {
- if id == 1 {
- Ok(String::from("alice"))
- } else {
- Err(format!("user {id} not found"))
- }
- }
- fn main() {
- let found = find_user(1);
- let name: Result<&str, &String> = found.as_deref();
- println!("{:?}", name);
- let missing = find_user(9);
- println!("{:?}", missing.as_deref());
- println!("{:?}", (3..3).is_empty());
- println!("{:?}", (1..=0).is_empty());
- }
輸出:
- Ok("alice")
- Err("user 9 not found")
- true
- true
TAU 常數
TAU 等於 2π,也就是一整圈的弧度。計算圓周長或角度換算時,用 TAU 比 2.0 * PI 更直接。範例:
- use std::f64::consts::{PI, TAU};
- fn main() {
- let r = 2.0;
- println!("{:.4}", TAU * r);
- println!("{}", TAU == 2.0 * PI);
- }
輸出:
- 12.5664
- true
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.47.0 的已知漏洞共 18 個;在本版修正的共 0 個;仍影響最新修補版 1.47.0 的共 18 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.47.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-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。