Rust 1.11
最後更新
Rust 1.11 發布於 2016-08-18,目前最新修補版為
1.11.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.11 的工作大多在編譯器內部,為 MIR 成為預設與增量編譯(incremental compilation)打基礎。使用者看得到的改變有:Cargo 支援 1.10 引入的
cdylib crate 類型、Iterator 新增 sum 與 product、BTreeMap 可以 append 與 split_off,HashMap 的預設雜湊從 SipHash 2-4 換成較快的 SipHash 1-3。升級要注意:const 與 static 不能是不定大小型別;macro_rules! 的 follow-set 限制(1.10 起為警告)正式生效。語言變更
- 支援巢狀的
cfg_attr屬性。 - 區塊結尾可以放產生陳述式的大括號巨集呼叫;巨集也能在特徵定義內展開,詳見 宣告式巨集。
- 由巨集展開出來的
#[macro_use]能正確運作。
標準庫重點
Iterator::sum與Iterator::product穩定,詳見 std::iter。BTreeMap、BTreeSet新增append與split_off,BinaryHeap新增append,詳見 BTreeMap。Cell::get_mut與RefCell::get_mut在已有可變參考時直接取得內部值,詳見 std::cell。assert_eq!和assert!一樣可以附上自訂錯誤訊息,詳見 單元測試。- 主執行緒的名稱改為
"main",不再是"<main>",詳見 std::thread。 thread_local!可以一次定義多個並套用屬性;Cow實作Default;範圍型別實作Hash。f32/f64的to_degrees、to_radians也放進 libcore。- 預設
HashMap雜湊器改為 SipHash 1-3,速度更快,仍足以防範碰撞攻擊,詳見 HashMap;Ipv4Addr的比較快 10 倍。
Cargo 與工具
- Cargo 支援
cdylib:在Cargo.toml寫crate-type = ["cdylib"]即可建置,詳見 FFI。 - 建置腳本可以輸出警告,
-vv會顯示所有 crate 的警告。 - 檔案有未提交變更(dirty)時禁止發布 crate;不再對
package.metadata鍵發出警告,留給其他工具擴充,詳見 Cargo。 - Windows 主控台支援顏色,stderr 的顏色也能設定。
- 更多錯誤有了錯誤代碼與延伸說明。
相容性注意
const與static不能是不定大小型別,詳見 常數。macro_rules!的 follow-set 規則正式啟用。cfg屬性的處理經過重構,少數邊緣情況會受影響。- 不能再指定遞移(transitive)相依的 features。
Iterator::sum 與 product
sum 把所有元素加總,product 把所有元素相乘。回傳型別通常要寫在變數上(例如 let total: i32),編譯器才知道要加總成哪種型別。空的迭代器 sum 得到 0、product 得到 1。範例:
- fn main() {
- let prices = [120, 80, 45];
- let total: i32 = prices.iter().sum();
- println!("總價 {}", total);
- let factorial: u64 = (1..=10).product();
- println!("10! = {}", factorial);
- let empty: Vec<i32> = Vec::new();
- let s: i32 = empty.iter().sum();
- let p: i32 = empty.iter().product();
- println!("空集合:sum = {}, product = {}", s, p);
- }
輸出:
- 總價 245
- 10! = 3628800
- 空集合:sum = 0, product = 1
注意:Rust 1.13 起
sum 與 product 沿用一般算術的溢位檢查,結果溢位時在除錯建置會 panic。BTreeMap 的 split_off 與 append
split_off(&key) 把大於等於 key 的項目切出去成為新的映射,原映射只留下較小的部分;append(&mut other) 把另一個映射的內容全部搬進來,other 會被清空。範例:
- use std::collections::BTreeMap;
- fn main() {
- let mut scores = BTreeMap::new();
- for (name, s) in [("amy", 72), ("ben", 91), ("cody", 58), ("dana", 85)] {
- scores.insert(name, s);
- }
- let second_half = scores.split_off("c");
- println!("前半 {:?}", scores);
- println!("後半 {:?}", second_half);
- let mut merged = second_half;
- merged.append(&mut scores);
- println!("合併 {:?},原本剩 {}", merged, scores.len());
- }
輸出:
- 前半 {"amy": 72, "ben": 91}
- 後半 {"cody": 58, "dana": 85}
- 合併 {"amy": 72, "ben": 91, "cody": 58, "dana": 85},原本剩 0
主執行緒名稱與 assert_eq! 自訂訊息
thread::current().name() 在主執行緒回傳 Some("main"),用 thread::Builder 建立的執行緒則是自訂的名稱,方便在日誌中辨識。assert_eq! 的第三個參數起是格式化的錯誤訊息,斷言失敗時一起印出。範例:
- use std::thread;
- fn main() {
- println!("{:?}", thread::current().name());
- let child = thread::Builder::new()
- .name("worker-1".to_string())
- .spawn(|| thread::current().name().map(|s| s.to_string()))
- .unwrap();
- println!("{:?}", child.join().unwrap());
- let a = 1 + 1;
- assert_eq!(a, 2, "算術壞了:a = {}", a);
- println!("assert_eq! 通過");
- }
輸出:
- Some("main")
- Some("worker-1")
- assert_eq! 通過
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.11.0 的已知漏洞共 17 個;在本版修正的共 0 個;仍影響最新修補版 1.11.0 的共 17 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.11.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-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。