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,所以從這版開始可以省略不寫。同一版的欄位初始化簡寫讓「變數名稱和欄位名稱一樣」時只寫一次,建構函式會清爽很多。範例:
- const APP: &str = "inventory";
- static UNITS: &[&str] = &["個", "箱"];
- struct Item {
- name: String,
- qty: u32,
- unit: &'static str,
- }
- fn make_item(name: &str, qty: u32) -> Item {
- let name = name.to_string();
- let unit = if qty >= 10 { UNITS[1] } else { UNITS[0] };
- Item { name, qty, unit }
- }
- fn main() {
- println!("{}", APP);
- for item in [make_item("螺絲", 3), make_item("螺帽", 12)] {
- println!("{} x {} {}", item.name, item.qty, item.unit);
- }
- }
輸出:
- inventory
- 螺絲 x 3 個
- 螺帽 x 12 箱
注意:結構欄位裡的參考不會自動變成
'static,像 unit: &'static str 仍要寫出生命週期。Ordering::then 與 then_with
排序常常需要「先比分數,同分再比年齡,再同就比名字」。
then 在前一個比較結果是 Equal 時才採用第二個結果;then_with 接收一個閉包,只有需要時才計算,適合比較成本較高的情況,詳見 閉包。範例:
- #[derive(Debug)]
- struct Player {
- name: &'static str,
- score: u32,
- age: u32,
- }
- fn main() {
- let mut players = vec![
- Player { name: "Ann", score: 90, age: 30 },
- Player { name: "Bob", score: 85, age: 25 },
- Player { name: "Cat", score: 90, age: 22 },
- Player { name: "Dan", score: 85, age: 25 },
- ];
- players.sort_by(|a, b| {
- b.score
- .cmp(&a.score)
- .then(a.age.cmp(&b.age))
- .then_with(|| a.name.cmp(b.name))
- });
- for p in &players {
- println!("{} {} {}", p.name, p.score, p.age);
- }
- }
輸出:
- Cat 90 22
- Ann 90 30
- Bob 85 25
- 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-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-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。