Rust 1.17

最後更新


Rust 1.17 發布於 2017-04-27,目前最新修補版為 1.17.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.17 主要是一連串讓日常寫法更順手的小改進:const 與 static 裡的參考預設就是 'static,不必再寫一長串生命週期;建立結構時欄位名稱和變數同名可以只寫一次。新手常犯的 "foo" + "bar" 也有了更清楚的錯誤訊息。升級時要注意:Windows 上這版無法自動偵測 Visual Studio 2017 的連結器,需要改用 VS 2015 或先執行 vcvars.bat。

語言變更


  • const 與 static 中的參考生命週期預設為 'static,const NAME: &str = "Ferris"; 即可,詳見 常數與靜態變數。
  • 欄位初始化簡寫(field init shorthand):Point { x, y } 等同 Point { x: x, y: y },詳見 結構。
  • impl 的 where 子句可以使用 Self。
  • 索引運算子 [] 的參數會自動強制轉型(coerce);靜態變數可以參考其他靜態變數。

標準庫重點


  • 新增 Ordering::then、Ordering::then_with,串接多個排序條件,詳見 std::cmp。
  • 新增 BTreeMap::range、BTreeMap::range_mut 與 collections::Bound,只走訪某個鍵的範圍,詳見 BTreeMap。
  • Cell 可以存放非 Copy 型別,並新增 Cell::swap、replace、into_inner、take,詳見 std::cell。
  • 新增 Rc/Arc 的 into_raw、from_raw、ptr_eq,詳見 std::rc。
  • 新增 Result::expect_err、process::abort、ptr::read_unaligned、ptr::write_unaligned。
  • Box 新增十多個 From 轉換(如 Box<str> 與 String 互轉);SocketAddr 可以從 ([127, 0, 0, 1], 3000) 轉換而來,詳見 std::net。
  • HashMap 會自適應地調整大小,防範 DoS 攻擊與品質差的雜湊函式。
  • 回溯(backtrace)預設精簡顯示,設定 RUST_BACKTRACE=full 可看完整內容,詳見 std::backtrace。

Cargo 與工具


  • 套件根目錄的 build.rs 會自動當作建置腳本,不想要時設定 build = false。
  • Cargo.toml 新增 required-features,指定某個執行檔或範例需要哪些 feature 才建置,詳見 Cargo.toml。
  • 新增 cargo check --all 與 cargo run --package。
  • 移除舊的 Makefile 建置系統;官方書籍改用 mdBook 產生,並新增記錄 nightly 功能的 Unstable Book。
  • rustup 恢復預設安裝本機文件。

相容性注意


  • Rust 1.17 無法正確偵測 MSVC 2017 連結器。
  • 強制轉型為大小不固定(unsized)的型別時,生命週期必須相等,這是健全性(soundness)修正。
  • format! 與 to_string 在底層的 Display 實作回傳錯誤時會 panic,以前會被默默忽略。
  • 含私有欄位的元組結構,其建構函式不再對外可見。

const/static 的 'static 省略與欄位初始化簡寫


const 與 static 的值在整個程式執行期間都存在,裡面的參考只可能是 'static,所以從這版開始可以省略不寫。同一版的欄位初始化簡寫讓「變數名稱和欄位名稱一樣」時只寫一次,建構函式會清爽很多。
範例:
  1. const APP: &str = "inventory";
  2. static UNITS: &[&str] = &["個", "箱"];
  3. struct Item {
  4. name: String,
  5. qty: u32,
  6. unit: &'static str,
  7. }
  8. fn make_item(name: &str, qty: u32) -> Item {
  9. let name = name.to_string();
  10. let unit = if qty >= 10 { UNITS[1] } else { UNITS[0] };
  11. Item { name, qty, unit }
  12. }
  13. fn main() {
  14. println!("{}", APP);
  15. for item in [make_item("螺絲", 3), make_item("螺帽", 12)] {
  16. println!("{} x {} {}", item.name, item.qty, item.unit);
  17. }
  18. }

輸出:
  1. inventory
  2. 螺絲 x 3 個
  3. 螺帽 x 12 箱

注意:結構欄位裡的參考不會自動變成 'static,像 unit: &'static str 仍要寫出生命週期。

Ordering::then 與 then_with


排序常常需要「先比分數,同分再比年齡,再同就比名字」。then 在前一個比較結果是 Equal 時才採用第二個結果;then_with 接收一個閉包,只有需要時才計算,適合比較成本較高的情況,詳見 閉包。
範例:
  1. #[derive(Debug)]
  2. struct Player {
  3. name: &'static str,
  4. score: u32,
  5. age: u32,
  6. }
  7. fn main() {
  8. let mut players = vec![
  9. Player { name: "Ann", score: 90, age: 30 },
  10. Player { name: "Bob", score: 85, age: 25 },
  11. Player { name: "Cat", score: 90, age: 22 },
  12. Player { name: "Dan", score: 85, age: 25 },
  13. ];
  14. players.sort_by(|a, b| {
  15. b.score
  16. .cmp(&a.score)
  17. .then(a.age.cmp(&b.age))
  18. .then_with(|| a.name.cmp(b.name))
  19. });
  20. for p in &players {
  21. println!("{} {} {}", p.name, p.score, p.age);
  22. }
  23. }

輸出:
  1. Cat 90 22
  2. Ann 90 30
  3. Bob 85 25
  4. Dan 85 25

注意:b.score.cmp(&a.score) 把 a、b 對調就是由大到小排序。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.17.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-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。

資料來源