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:可以是單一字元、字串切片,也可以是「判斷字元」的閉包或函式。同一個方法就能涵蓋「找某個字」「找某段字」「找符合條件的字」三種需求。範例:
- fn main() {
- let text = "Rust 1.0 stable, 2015-05-15";
- println!("{:?}", text.find('1'));
- println!("{:?}", text.find("stable"));
- println!("{:?}", text.find(|c: char| c == ','));
- println!("{:?}", text.find(char::is_whitespace));
- let parts: Vec<&str> = text.split(|c: char| c == ' ' || c == ',').collect();
- println!("{:?}", parts);
- }
輸出:
- Some(5)
- Some(9)
- Some(15)
- Some(4)
- ["Rust", "1.0", "stable", "", "2015-05-15"]
注意:
find 回傳的是位元組索引(byte index),不是第幾個字元;字串含中文等多位元組字元時,兩者不同。From 與 Into 型別轉換
1.0 把型別轉換的慣例集中到
std::convert:為目標型別實作 From,就自動得到反方向的 Into。函式參數寫成 T: Into<目標型別>,呼叫端就能直接傳入任何可轉換的值。範例:
- struct Celsius(f64);
- struct Fahrenheit(f64);
- impl From<Celsius> for Fahrenheit {
- fn from(c: Celsius) -> Self {
- Fahrenheit(c.0 * 9.0 / 5.0 + 32.0)
- }
- }
- fn describe<T: Into<Fahrenheit>>(t: T) -> String {
- format!("{}°F", t.into().0)
- }
- fn main() {
- let f = Fahrenheit::from(Celsius(100.0));
- println!("{}", f.0);
- println!("{}", describe(Celsius(-40.0)));
- let name = String::from("ferris");
- let bytes: Vec<u8> = name.into();
- println!("{:?}", bytes);
- }
輸出:
- 212
- -40°F
- [102, 101, 114, 114, 105, 115]
以值索引 HashMap
Index 改為以值接收索引後,HashMap<String, _> 可以直接用字串切片 map["key"] 取值,寫法和陣列、Vec 的 v[1] 一致。範例:
- use std::collections::HashMap;
- fn main() {
- let mut ages = HashMap::new();
- ages.insert(String::from("ferris"), 9);
- ages.insert(String::from("corro"), 5);
- println!("ferris 今年 {} 歲", ages["ferris"]);
- println!("{:?}", ages.get("nobody"));
- let v = vec![10, 20, 30];
- println!("{}", v[1]);
- }
輸出:
- ferris 今年 9 歲
- None
- 20
注意:
map["key"] 找不到鍵時會 panic;不確定鍵是否存在時,改用回傳 Option 的 get。本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
本版沒有修補版本。
已知漏洞
影響
1.0.0 的已知漏洞共 15 個;在本版修正的共 0 個;仍影響最新修補版 1.0.0 的共 15 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.0.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-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-1000622 | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | 1.27.1 |
| CVE-2017-20004 | std | MutexGuard<Cell<i32>> must not be Sync | 早於 1.19.0 | 1.19.0 |
| CVE-2015-20001 | std | Panic safety violation in BinaryHeap | 早於 1.2.0 | 1.2.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。