Rust 1.14
最後更新
Rust 1.14 發布於 2016-12-22,目前最新修補版為
1.14.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.14 最大的消息不在語言本身:rustup 發布 1.0,成為官方建議的安裝方式,可以輕鬆切換 stable、beta、nightly 並加裝交叉編譯用的標準庫。這版也以實驗方式支援 WebAssembly 目標
wasm32-unknown-emscripten,並新增 MIPS、PowerPC、s390x 等主機平台。語言方面,.. 可以在元組與元組結構的模式中略過任意數量的欄位。升級要注意:多個前向相容 lint 改為預設拒絕(deny),以前只是警告的程式碼現在會編譯失敗;rustup 預設不再安裝文件,需要時執行 rustup component add rust-docs。語言變更
..可以在列舉變體、結構與元組的模式中比對多個元組欄位(RFC 1492),詳見 模式。- 安全的
fn項目可以轉型成unsafe fn指標,詳見 unsafe。 use *與use ::*都從 crate 根部做 glob 匯入,詳見 模組系統。Vec<Box<Fn()>>裡的閉包不必明確解參考就能呼叫,詳見 閉包。
標準庫重點
println!()不帶任何引數時印出一個換行,詳見 std::fmt。String實作From<Cow<str>>、Vec<T>實作From<Cow<[T]>>;Cow<str>可以用+接上&str或Cow<str>,詳見 std::borrow。Wrapping實作標準的二元與一元運算子,以及Sum、Product,詳見 std::num。io::Error實作From<io::ErrorKind>,詳見 std::io。HashMap的內部記憶體配置對快取更友善,部分操作明顯加快,也不再重複使用隨機種子,詳見 HashMap。- 原子型別實作
RefUnwindSafe;ReadDir實作Debug;Vec::extend遇到切片時特化成extend_from_slice。
Cargo 與工具
- rustup 是官方建議的安裝方式,詳見 安裝。
- 實驗性支援 WebAssembly:
rustup target add wasm32-unknown-emscripten後即可用rustc --target=wasm32-unknown-emscripten編譯,已知仍有重大缺陷。 - 新增 MIPS、PowerPC、s390x 的 Linux 主機建置,以及 ARM MUSL 的標準庫建置,都是 tier 2 平台。
- Cargo 把
rustc --print cfg的值傳給建置腳本;CARGO_HOME唯讀時也能運作;測試與 bench 設定檔忽略panic設定,避免panic=abort讓單一測試失敗中止整個測試套件,詳見 Cargo。 - Rust 發行版啟用行號除錯資訊;
rustc --version --verbose會顯示 LLVM 版本。
相容性注意
- 多個前向相容 lint 改為預設拒絕,例如浮點數常數不能用在模式中、結構或列舉常數必須
#[derive(PartialEq, Eq)]才能用於模式。 - 原子型別的
Ordering列舉不能再窮舉比對,詳見 std::sync::atomic。 - 沒有函式本體的特徵方法不能在參數使用模式;列舉不能是不定大小型別。
- 實作比特徵本身要求更多生命週期條件的
impl會被拒絕。 $crate巨集變數可使用的位置變少,詳見 宣告式巨集。
模式中的 .. 略過部分欄位
以前元組結構只能用
Point(..) 一次略過全部欄位,想取第一個欄位就得把其他欄位都寫成 _。1.14 起 .. 可以放在開頭、中間或結尾,只取需要的欄位,寫法和具名欄位結構的 Point { x, .. } 一致。範例:
- struct Rgb(u8, u8, u8);
- enum Shape {
- Rect(u32, u32, &'static str),
- }
- fn main() {
- let c = Rgb(255, 128, 0);
- let Rgb(red, ..) = c;
- let Rgb(_, green, ..) = c;
- let Rgb(.., blue) = c;
- println!("red = {}, green = {}, blue = {}", red, green, blue);
- let t = (1, 2, 3, 4, 5);
- let (first, .., last) = t;
- println!("{} {}", first, last);
- let s = Shape::Rect(3, 4, "藍");
- match s {
- Shape::Rect(w, h, ..) => println!("面積 {}", w * h),
- }
- let Shape::Rect(.., color) = s;
- println!("顏色 {}", color);
- }
輸出:
- red = 255, green = 128, blue = 0
- 1 5
- 面積 12
- 顏色 藍
注意:一個模式裡只能出現一次
..,寫成 (a, .., b, .., c) 會因為無法判斷略過哪些欄位而編譯失敗。println!() 與 Cow 轉換
println!() 不帶引數就印出空行,不必再寫 println!("")。Cow<str> 表示「可能借用、也可能擁有」的字串;1.14 起它能直接用 into() 或 String::from 轉成 String,也能用 + 接上字串切片。範例:
- use std::borrow::Cow;
- fn normalize(input: &str) -> Cow<'_, str> {
- if input.contains(' ') {
- Cow::Owned(input.replace(' ', "_"))
- } else {
- Cow::Borrowed(input)
- }
- }
- fn main() {
- println!("第一行");
- println!();
- println!("空一行之後");
- let a: String = normalize("hello world").into();
- let b: String = String::from(normalize("rust"));
- println!("{} {}", a, b);
- let tail = Cow::Borrowed("abc") + "def";
- println!("{}", tail);
- }
輸出:
- 第一行
- 空一行之後
- hello_world rust
- abcdef
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.14.0 的已知漏洞共 20 個;在本版修正的共 0 個;仍影響最新修補版 1.14.0 的共 20 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.14.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-28877 | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起,早於 1.51.0 | 1.51.0 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
| CVE-2019-16760 | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | 1.26.0 |
| CVE-2018-1000657 | std | Buffer overflow vulnerability in VecDeque::reserve() | 1.3.0 起,早於 1.22.0 | 1.22.0 |
| CVE-2018-1000622 | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | 1.27.1 |
| CVE-2018-25008 | std | Insufficient synchronization in Arc::get_mut | 1.3.0 起,早於 1.29.0 | 1.29.0 |
| CVE-2017-20004 | std | MutexGuard<Cell<i32>> must not be Sync | 早於 1.19.0 | 1.19.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。