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。
範例:
  1. fn main() {
  2. let prices = [120, 80, 45];
  3. let total: i32 = prices.iter().sum();
  4. println!("總價 {}", total);
  5. let factorial: u64 = (1..=10).product();
  6. println!("10! = {}", factorial);
  7. let empty: Vec<i32> = Vec::new();
  8. let s: i32 = empty.iter().sum();
  9. let p: i32 = empty.iter().product();
  10. println!("空集合:sum = {}, product = {}", s, p);
  11. }

輸出:
  1. 總價 245
  2. 10! = 3628800
  3. 空集合:sum = 0, product = 1

注意:Rust 1.13 起 sum 與 product 沿用一般算術的溢位檢查,結果溢位時在除錯建置會 panic。

BTreeMap 的 split_off 與 append


split_off(&key) 把大於等於 key 的項目切出去成為新的映射,原映射只留下較小的部分;append(&mut other) 把另一個映射的內容全部搬進來,other 會被清空。
範例:
  1. use std::collections::BTreeMap;
  2. fn main() {
  3. let mut scores = BTreeMap::new();
  4. for (name, s) in [("amy", 72), ("ben", 91), ("cody", 58), ("dana", 85)] {
  5. scores.insert(name, s);
  6. }
  7. let second_half = scores.split_off("c");
  8. println!("前半 {:?}", scores);
  9. println!("後半 {:?}", second_half);
  10. let mut merged = second_half;
  11. merged.append(&mut scores);
  12. println!("合併 {:?},原本剩 {}", merged, scores.len());
  13. }

輸出:
  1. 前半 {"amy": 72, "ben": 91}
  2. 後半 {"cody": 58, "dana": 85}
  3. 合併 {"amy": 72, "ben": 91, "cody": 58, "dana": 85},原本剩 0

主執行緒名稱與 assert_eq! 自訂訊息


thread::current().name() 在主執行緒回傳 Some("main"),用 thread::Builder 建立的執行緒則是自訂的名稱,方便在日誌中辨識。assert_eq! 的第三個參數起是格式化的錯誤訊息,斷言失敗時一起印出。
範例:
  1. use std::thread;
  2. fn main() {
  3. println!("{:?}", thread::current().name());
  4. let child = thread::Builder::new()
  5. .name("worker-1".to_string())
  6. .spawn(|| thread::current().name().map(|s| s.to_string()))
  7. .unwrap();
  8. println!("{:?}", child.join().unwrap());
  9. let a = 1 + 1;
  10. assert_eq!(a, 2, "算術壞了:a = {}", a);
  11. println!("assert_eq! 通過");
  12. }

輸出:
  1. Some("main")
  2. Some("worker-1")
  3. assert_eq! 通過

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


影響 1.11.0 的已知漏洞共 17 個;在本版修正的共 0 個;仍影響最新修補版 1.11.0 的共 17 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。

仍影響 1.11.0


編號元件摘要(官方原文)影響範圍修正於
CVE-2026-5222cargoCargo can be coerced to share credentials between registries早於 1.96.01.96.0
CVE-2026-5223cargoCrates in third party registries can override the cached source of other crates早於 1.96.01.96.0
CVE-2024-43402stdstd::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods早於 1.81.01.81.0
CVE-2024-24576stdstd::process::Command did not properly escape arguments of batch files on Windows早於 1.77.21.77.2
CVE-2023-38497cargoCargo not respecting umask when extracting crate archives早於 1.71.11.71.1
CVE-2022-46176cargoCargo did not verify SSH host keys早於 1.66.11.66.1
CVE-2022-36113cargoExtracting malicious crates can corrupt arbitrary files早於 1.64.01.64.0
CVE-2022-36114cargoExtracting malicious crates can fill the file system早於 1.64.01.64.0
CVE-2022-21658stdRace condition in std::fs::remove_dir_all早於 1.58.11.58.1
CVE-2021-42574rustcrustc and bidirectional-override codepoints in source code早於 1.56.11.56.1
CVE-2021-28877stdTrustedRandomAccess specialization composes incorrectly for nested iter::Zips1.11.0 起,早於 1.51.01.51.0
CVE-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0
CVE-2019-16760cargoCargo prior to Rust 1.26.0 may download the wrong dependency早於 1.26.01.26.0
CVE-2018-1000657stdBuffer overflow vulnerability in VecDeque::reserve()1.3.0 起,早於 1.22.01.22.0
CVE-2018-1000622rustdocUncontrolled search path element vulnerability in rustdoc plugins早於 1.27.11.27.1
CVE-2018-25008stdInsufficient synchronization in Arc::get_mut1.3.0 起,早於 1.29.01.29.0
CVE-2017-20004stdMutexGuard<Cell<i32>> must not be Sync早於 1.19.01.19.0

本版已停止支援,不會再修正這些漏洞;請升級到 1.100.1。

資料來源