Rust 1.25
最後更新
Rust 1.25 發布於 2018-03-29,目前最新修補版為
1.25.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.25 讓
use 可以巢狀分組,例如 use std::{fs::File, io::Read, path::{Path, PathBuf}};,一行就能整理一整串匯入;match 分支也允許以 | 開頭。對新手最直接的改變是 cargo new 預設建立可執行的程式(binary)而不是函式庫,建好就能 cargo run。其他重點包括升級到 LLVM 6、穩定 #[repr(align(N))] 與 ptr::NonNull,以及 Rust by Example 隨發行版一起提供。語言變更
use支援巢狀匯入群組,詳見 模組系統。match分支可以用|開頭,方便多行排列多個模式,詳見 match。#[repr(align(N))]穩定,可以指定型別的對齊方式,詳見 std::mem。
標準庫重點
- 新增
ptr::NonNull<T>:保證不為空的原始指標,Option<NonNull<T>>和*mut T一樣大,詳見 std::ptr。 Duration::new、Duration::from_secs、Duration::from_millis可以在常數運算式中使用;Duration移到 libcore,詳見 std::time。- 新增
Location::column,詳見 std::panic。 UnsafeCell::into_inner改為安全函式;Unix 上的process::Command實作Send。Cursor<&mut Vec<u8>>實作Write,詳見 std::io;Component實作AsRef<Path>。
Cargo 與工具
cargo new預設建立 binary crate,要函式庫請加--lib,詳見 Cargo。cargo new不再自動去掉名稱中的rust-前綴或-rs後綴。cargo doc改用cargo check的方式產生,部分情況更快;git 相依盡可能使用硬連結,簽出更快。- rustdoc 預設改用 pulldown-cmark 解析 Markdown,完全遵循 CommonMark,詳見 文件註解。
- 新增
-C lto=val編譯選項。
相容性注意
- 尚未穩定的
net::lookup_host已棄用,穩定版程式改用ToSocketAddrs,詳見 std::net。 - 修正借用檢查器在索引運算附近錯誤允許重疊借用的問題,同時讓一些原本被誤判的正確程式碼可以編譯。
- 移除已棄用的不穩定屬性
#[simd]。
巢狀匯入與 match 開頭的 |
同一個 crate 底下的多個路徑,現在可以用大括號層層分組,一個
use 就寫完。match 分支開頭也可以多寫一個 |,多個模式垂直排列時每行看起來一致。範例:
- use std::{
- collections::{btree_map::Entry, BTreeMap},
- fmt::Write,
- };
- enum Level {
- Debug,
- Info,
- Warn,
- Error,
- }
- fn label(level: &Level) -> &'static str {
- match level {
- | Level::Debug
- | Level::Info => "一般",
- | Level::Warn
- | Level::Error => "需要注意",
- }
- }
- fn main() {
- let mut counts = BTreeMap::new();
- for level in [Level::Info, Level::Warn, Level::Debug, Level::Error, Level::Info] {
- match counts.entry(label(&level)) {
- Entry::Occupied(mut e) => *e.get_mut() += 1,
- Entry::Vacant(e) => {
- e.insert(1);
- }
- }
- }
- let mut report = String::new();
- write!(report, "{:?}", counts).unwrap();
- println!("{}", report);
- }
輸出:
- {"一般": 3, "需要注意": 2}
注意:
use std::fmt::Write 匯入的是特徵,write! 寫進 String 需要它在作用域內;匯入特徵後才能呼叫該特徵的方法。#[repr(align(N))]
對齊(alignment)決定一個值可以放在哪些記憶體位址:對齊 16 表示位址必須是 16 的倍數。
#[repr(align(N))] 讓你把型別的對齊提高到 N(必須是 2 的次方),常見用途是讓資料剛好占一條 CPU 快取線(cache line),避免多執行緒互相干擾,或符合 SIMD 與硬體介面的要求。型別大小會被補齊到對齊的倍數。以下數值為 64 位元平台的結果。
範例:
- use std::mem::{align_of, size_of};
- #[allow(dead_code)]
- struct Plain(i32);
- #[allow(dead_code)]
- #[repr(align(16))]
- struct Aligned16(i32);
- #[allow(dead_code)]
- #[repr(align(64))]
- struct CacheLine {
- counter: u64,
- }
- fn main() {
- println!("Plain:align {},size {}", align_of::<Plain>(), size_of::<Plain>());
- println!("Aligned16:align {},size {}", align_of::<Aligned16>(), size_of::<Aligned16>());
- println!("CacheLine:align {},size {}", align_of::<CacheLine>(), size_of::<CacheLine>());
- let items = [Aligned16(1), Aligned16(2)];
- let a = &items[0] as *const Aligned16 as usize;
- let b = &items[1] as *const Aligned16 as usize;
- println!("相鄰元素相距 {} bytes,位址是 16 的倍數:{}", b - a, a % 16 == 0);
- }
輸出:
- Plain:align 4,size 4
- Aligned16:align 16,size 16
- CacheLine:align 64,size 64
- 相鄰元素相距 16 bytes,位址是 16 的倍數:true
注意:
align 只能提高對齊,不能降低;N 不是 2 的次方會編譯失敗。要降低對齊請用 #[repr(packed)],但存取其欄位需要特別小心。本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.25.0 的已知漏洞共 20 個;在本版修正的共 0 個;仍影響最新修補版 1.25.0 的共 20 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.25.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-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-2019-1010299 | std | vec_deque::Iter has unsound Debug implementation | 1.18.0 起,早於 1.30.0 | 1.30.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 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。