Rust 1.0

最後更新


Rust 1.0 發布於 2015-05-15,目前最新修補版為 1.0.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.0 是第一個穩定版,標誌著語言與標準庫結束「隨時大改」的實驗期,官方從這版起正式承諾穩定性:之後的版本原則上不做破壞性變更(編譯器 bug 等少數情況例外),1.0 寫的程式在新版編譯器上仍能編譯。發布方式也改成每 6 週出一版的「火車模式」(train-based release model),1.0 與 1.1 beta 同一天推出。對新手來說,這代表從 1.0 起學到的寫法都有效;1.0 之前的教學與範例可能已經過時。升級要注意:穩定版編譯器不再允許使用不穩定功能,要試驗新功能得改用 nightly。

語言變更


  • 基本整數型別的算術在除錯(debug)建置下會檢查溢位,溢位就 panic,詳見 整數。
  • 原始型別有了固有方法(inherent method),不必再匯入 SliceExt 這類擴充特徵;MyType::default() 這種不寫特徵名稱的關聯路徑也能使用。
  • crate 名稱不能再含 -;Cargo 會自動把套件名稱的 - 轉成 _ 作為 crate 名稱,詳見 Cargo。
  • 生命週期遮蔽(lifetime shadowing)改為編譯錯誤,詳見 生命週期。
  • Send 不再隱含 'static;Send、Sync 改由標準庫定義,詳見 Send 與 Sync。
  • 二進位與八進位字面值會一次讀完整串數字,例如 0b1234 被視為一個字面值,而不是 0b1 和 234 兩個。

標準庫重點


  • AsRef、AsMut、From、Into 集中到 std::convert,FromError 由 From 取代,詳見 std::convert。
  • IteratorExt 的方法併入 Iterator 特徵本身,詳見 std::iter。
  • 字串搜尋方法改成接受 Pattern,可以傳字元、字串切片或閉包,詳見 &str。
  • 新的路徑與 I/O 模組完成並標為穩定,詳見 std::path 與 std::io。
  • Index/IndexMut 改為以值接收索引,因此能寫 hash_map["string"],詳見 HashMap。
  • Copy 改為繼承 Clone,所有 Copy 的資料一定也是 Clone,詳見 移動與複製。
  • Fn 系列特徵之間有了繼承關係,詳見 閉包。
  • &str 的 words 由 split_whitespace 取代;String::from_str 改用 String::from。

Cargo 與工具


  • Cargo 負責下載與建置相依套件,crates.io 是集中的套件倉庫,許多熱門 crate 已能在穩定版上使用,詳見 Cargo。
  • 許多錯誤有延伸說明,可用 rustc --explain 查看。
  • metadata 經過調整,二進位檔縮小 27%。

相容性注意


  • 穩定版編譯器無法使用不穩定功能;大部分標準庫已標為 #[stable]。
  • 特徵一致性(trait coherence)規則加上限制,讓上游修改特徵時比較不會破壞下游程式碼。
  • splitn 的 n 改為代表「回傳的項目數」,而不是切割次數。
  • 衍生(derive)的 PartialOrd 會依列舉明確指定的判別值(discriminant)排序。
  • Unix 上所有檔案描述符預設帶 CLOEXEC。

字串搜尋的 Pattern


find、split 這類字串搜尋方法接受的參數稱為 Pattern:可以是單一字元、字串切片,也可以是「判斷字元」的閉包或函式。同一個方法就能涵蓋「找某個字」「找某段字」「找符合條件的字」三種需求。
範例:
  1. fn main() {
  2. let text = "Rust 1.0 stable, 2015-05-15";
  3. println!("{:?}", text.find('1'));
  4. println!("{:?}", text.find("stable"));
  5. println!("{:?}", text.find(|c: char| c == ','));
  6. println!("{:?}", text.find(char::is_whitespace));
  7. let parts: Vec<&str> = text.split(|c: char| c == ' ' || c == ',').collect();
  8. println!("{:?}", parts);
  9. }

輸出:
  1. Some(5)
  2. Some(9)
  3. Some(15)
  4. Some(4)
  5. ["Rust", "1.0", "stable", "", "2015-05-15"]

注意:find 回傳的是位元組索引(byte index),不是第幾個字元;字串含中文等多位元組字元時,兩者不同。

From 與 Into 型別轉換


1.0 把型別轉換的慣例集中到 std::convert:為目標型別實作 From,就自動得到反方向的 Into。函式參數寫成 T: Into<目標型別>,呼叫端就能直接傳入任何可轉換的值。
範例:
  1. struct Celsius(f64);
  2. struct Fahrenheit(f64);
  3. impl From<Celsius> for Fahrenheit {
  4. fn from(c: Celsius) -> Self {
  5. Fahrenheit(c.0 * 9.0 / 5.0 + 32.0)
  6. }
  7. }
  8. fn describe<T: Into<Fahrenheit>>(t: T) -> String {
  9. format!("{}°F", t.into().0)
  10. }
  11. fn main() {
  12. let f = Fahrenheit::from(Celsius(100.0));
  13. println!("{}", f.0);
  14. println!("{}", describe(Celsius(-40.0)));
  15. let name = String::from("ferris");
  16. let bytes: Vec<u8> = name.into();
  17. println!("{:?}", bytes);
  18. }

輸出:
  1. 212
  2. -40°F
  3. [102, 101, 114, 114, 105, 115]

以值索引 HashMap


Index 改為以值接收索引後,HashMap<String, _> 可以直接用字串切片 map["key"] 取值,寫法和陣列、Vec 的 v[1] 一致。
範例:
  1. use std::collections::HashMap;
  2. fn main() {
  3. let mut ages = HashMap::new();
  4. ages.insert(String::from("ferris"), 9);
  5. ages.insert(String::from("corro"), 5);
  6. println!("ferris 今年 {} 歲", ages["ferris"]);
  7. println!("{:?}", ages.get("nobody"));
  8. let v = vec![10, 20, 30];
  9. println!("{}", v[1]);
  10. }

輸出:
  1. ferris 今年 9 歲
  2. None
  3. 20

注意:map["key"] 找不到鍵時會 panic;不確定鍵是否存在時,改用回傳 Option 的 get。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.0.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-1000622rustdocUncontrolled search path element vulnerability in rustdoc plugins早於 1.27.11.27.1
CVE-2017-20004stdMutexGuard<Cell<i32>> must not be Sync早於 1.19.01.19.0
CVE-2015-20001stdPanic safety violation in BinaryHeap早於 1.2.01.2.0

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

資料來源