Rust 1.20

最後更新


Rust 1.20 發布於 2017-08-31,目前最新修補版為 1.20.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.20 穩定了關聯常數(associated constant):型別與特徵除了關聯函式之外,也能掛上常數,特徵還能只宣告常數、由實作者決定值。標準庫新增不穩定排序 sort_unstable 系列,以及不會 panic 的字串切片 str::get。Cargo 把 crates.io 的登入權杖從 ~/.cargo/config 移到 ~/.cargo/credentials,方便設成只有自己能讀。

語言變更


  • 結構、列舉與特徵可以定義關聯常數,詳見 特徵進階。
  • 修正大量巨集相關的錯誤,巨集展開速度提升 29 倍,詳見 宣告式巨集。
  • 編譯失敗時回報的錯誤總數改為全部錯誤,而不是只算失敗的那個階段。

標準庫重點


  • 新增 slice::sort_unstable、sort_unstable_by、sort_unstable_by_key,詳見 切片。
  • 新增 str::get、str::get_mut,範圍不合法時回傳 None 而不是 panic,詳見 str。
  • 新增 Option::get_or_insert、Option::get_or_insert_with,詳見 Option。
  • 新增 f32/f64 的 to_bits、from_bits,詳見 f64 與浮點數。
  • 新增 mem::ManuallyDrop,手動控制何時執行解構,詳見 std::mem。
  • 新增 compile_error! 巨集;unimplemented!() 可以附上訊息;char 實作 FromStr。
  • Ref、RefMut、MutexGuard 與 RwLock 的守衛(guard)實作 Display 與 Debug;Unicode 升級到 10.0.0。
  • 加入防範 Stack Clash 的措施(stack probe 等),升級即生效。

Cargo 與工具


  • crates.io 權杖改存於 ~/.cargo/credentials。
  • src/bin/server/main.rs 這種子目錄形式會建置成名為 server 的執行檔,詳見 Cargo。
  • cargo install 可以用 --vers 指定版本;新增 --no-fail-fast,benchmark 失敗也繼續跑完。

相容性注意


  • f32/f64 的 is_sign_negative、is_sign_positive 改為會考慮 NaN 的正負號。
  • 回傳型別寫 'static 的函式,不再能像使用生命週期參數時那樣通用。

關聯常數


關聯常數寫在 impl 或特徵裡,用 型別::常數 取用。特徵可以只宣告 const SIDES: u32; 不給值,每個實作各自填入;預設方法裡則可以用 Self::SIDES 讀到實作者的值。因為是常數,它也能用在陣列長度這類需要編譯期值的地方。
範例中的 u8::MAX 也是關聯常數,不過整數型別的這類常數是 Rust 1.43 起才有的。
範例:
  1. trait Shape {
  2. const SIDES: u32;
  3. const NAME: &'static str;
  4. fn describe(&self) -> String {
  5. format!("{} 有 {} 條邊", Self::NAME, Self::SIDES)
  6. }
  7. }
  8. struct Triangle;
  9. struct Square;
  10. impl Shape for Triangle {
  11. const SIDES: u32 = 3;
  12. const NAME: &'static str = "三角形";
  13. }
  14. impl Shape for Square {
  15. const SIDES: u32 = 4;
  16. const NAME: &'static str = "正方形";
  17. }
  18. struct Buffer;
  19. impl Buffer {
  20. const CAPACITY: usize = 4;
  21. }
  22. fn main() {
  23. println!("{}", Triangle.describe());
  24. println!("{}", Square.describe());
  25. let slots = [0u8; Buffer::CAPACITY];
  26. println!("容量 {},陣列長度 {}", Buffer::CAPACITY, slots.len());
  27. println!("u8 最大值:{}", u8::MAX);
  28. }

輸出:
  1. 三角形 有 3 條邊
  2. 正方形 有 4 條邊
  3. 容量 4,陣列長度 4
  4. u8 最大值:255

注意:實作特徵時漏掉沒有預設值的關聯常數會編譯失敗,和漏掉必要方法一樣。

sort_unstable 系列


「穩定」排序指的是:比較結果相等的元素,排序後仍維持原本的先後順序。sort 是穩定的;sort_unstable 不保證這點,但通常更快,而且不需要額外配置記憶體。只要不在意相等元素的順序(例如排數字),就可以選它。
範例:
  1. fn main() {
  2. let words = ["rust", "crate", "package", "cargo"];
  3. let mut stable = words;
  4. stable.sort_by_key(|w| w.as_bytes()[0]);
  5. println!("穩定排序:{:?}", stable);
  6. let mut nums = [42, 7, 19, 3, 25, 7];
  7. nums.sort_unstable();
  8. println!("不穩定排序:{:?}", nums);
  9. let mut by_len = words;
  10. by_len.sort_unstable_by(|a, b| b.len().cmp(&a.len()).then(a.cmp(b)));
  11. println!("依長度由長到短:{:?}", by_len);
  12. }

輸出:
  1. 穩定排序:["crate", "cargo", "package", "rust"]
  2. 不穩定排序:[3, 7, 7, 19, 25, 42]
  3. 依長度由長到短:["package", "cargo", "crate", "rust"]

注意:只用首字母排序時,crate 和 cargo 算相等;穩定排序保證 crate 仍在前,換成 sort_unstable_by_key 就沒有這個保證。範例的第三段把比較條件補到完全分出先後,結果才確定。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.20.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-28878stdZip may call __iterator_get_unchecked twice with the same index1.14.0 起,早於 1.52.01.52.0
CVE-2021-28879stdZip can cause buffer overflow when a consumed Zip iterator is used again1.14.0 起,早於 1.52.01.52.0
CVE-2021-28876stdPanic safety issue in Zip specialization1.14.0 起,早於 1.52.01.52.0
CVE-2021-28875stdLogic bug in Read can cause buffer overflow in read_to_end()1.20.0 起,早於 1.50.01.50.0
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-2019-1010299stdvec_deque::Iter has unsound Debug implementation1.18.0 起,早於 1.30.01.30.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

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

資料來源