Rust 1.5

最後更新


Rust 1.5 發布於 2015-12-10,目前最新修補版為 1.5.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.5 最大的消息是 cargo install:可以把 crates.io 上的 Rust 應用程式安裝到 ~/.cargo/bin,名為 cargo-foo 的執行檔還能直接用 cargo foo 當成 Cargo 子命令呼叫。標準庫穩定了大量路徑與檔案系統相關的 API(Path::exists、Path::is_file、fs::canonicalize 等),crate metadata 也縮小約 20%。升級要注意:發布到 crates.io 時使用萬用字元(*)版本的相依會出現警告,1.6 起直接禁止。

語言變更


  • 在非常數情境下於編譯期求值時,過長的位移或算術溢位改為警告而非錯誤,錯誤延到執行期才發生。
  • improper_ctypes lint 不再對 FFI 中使用 isize/usize 發出警告,詳見 FFI。

標準庫重點


  • Path 新增 exists、is_file、is_dir、metadata、read_dir、canonicalize 等方法,詳見 std::path。
  • Vec::resize 把長度調整成指定值,不足的部分以給定值補上,詳見 Vec。
  • 切片新增 split_first、split_last,詳見 切片。
  • str::match_indices 與 rmatch_indices 回傳每個符合位置與內容,詳見 &str。
  • Iterator 新增 eq、ne、lt、le、gt、ge、cmp、partial_cmp,可以逐元素比較兩個迭代器,詳見 std::iter。
  • Formatter 新增 width、precision、fill、alternate 等,自訂格式化時能讀取格式參數,詳見 std::fmt。
  • 整數之間、浮點數之間在不損失資訊時提供 From 轉換,詳見 型別轉換。
  • 解析整數時接受開頭的 +;BinaryHeap::into_sorted_vec、Condvar::wait_timeout 等穩定。
  • Arc<T> 與 Rc<T> 對 T 改為共變(covariant)。

Cargo 與工具


  • 新增 cargo install,安裝 crates.io 上的應用程式(例如 rustfmt)或新的 Cargo 子命令,詳見 Cargo 工具。
  • 發布含萬用字元相依的 crate 會出現警告。

相容性注意


  • dropck 判斷生命週期的規則不再依賴參數化(parametricity)。
  • Box、Rc、Arc 新增 AsRef/AsMut 實作;內部型別有同名方法時可能產生衝突。
  • 長期棄用的 #[derive(Show)] 已移除。
  • 對無號整數取負號會產生錯誤。
  • 父特徵(supertrait)含有 Self 的特徵不再是物件安全(object safe),詳見 特徵物件。

Vec::resize 與 split_first


resize(n, value) 把 Vec 調整成剛好 n 個元素:變長時用 value 補,變短時截掉尾端。split_first 一次取出「第一個元素」與「其餘部分」,空切片時回傳 None,比手動檢查長度再索引安全。
範例:
  1. fn main() {
  2. let mut scores = vec![90, 85];
  3. scores.resize(5, 0);
  4. println!("{:?}", scores);
  5. scores.resize(3, 0);
  6. println!("{:?}", scores);
  7. if let Some((first, rest)) = scores.split_first() {
  8. println!("第一個 {},其餘 {:?}", first, rest);
  9. }
  10. let empty: [i32; 0] = [];
  11. println!("{:?}", empty.split_last());
  12. }

輸出:
  1. [90, 85, 0, 0, 0]
  2. [90, 85, 0]
  3. 第一個 90,其餘 [85, 0]
  4. None

match_indices 找出所有位置


find 只回傳第一個符合的位置;match_indices 回傳每個符合的 (位置, 內容),rmatch_indices 則從尾端往前找。整數解析接受 + 號也一併示範。
範例:
  1. fn main() {
  2. let log = "ok,err,ok,ok,err";
  3. let errs: Vec<(usize, &str)> = log.match_indices("err").collect();
  4. println!("{:?}", errs);
  5. let last_ok: Vec<(usize, &str)> = log.rmatch_indices("ok").take(1).collect();
  6. println!("{:?}", last_ok);
  7. println!("{:?}", "+42".parse::<i32>());
  8. }

輸出:
  1. [(3, "err"), (13, "err")]
  2. [(10, "ok")]
  3. Ok(42)

比較兩個迭代器


迭代器的 eq、cmp 會逐一比較元素,不必先收集成 Vec。下面用它比較版本號:把每段轉成數字再比,1.4.0 小於 1.10.0;直接比較字串則會依字典序得到相反的結果。
範例:
  1. use std::cmp::Ordering;
  2. fn main() {
  3. let a = [1, 2, 3];
  4. let v = vec![1, 2, 3];
  5. println!("{}", a.iter().eq(v.iter()));
  6. let version_a = "1.4.0".split('.').map(|s| s.parse::<u32>().unwrap());
  7. let version_b = "1.10.0".split('.').map(|s| s.parse::<u32>().unwrap());
  8. let ord = version_a.cmp(version_b);
  9. println!("{:?}", ord);
  10. println!("{}", ord == Ordering::Less);
  11. println!("{}", "1.4.0" < "1.10.0");
  12. }

輸出:
  1. true
  2. Less
  3. true
  4. false

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源