# 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，詳見 [整數](/types-integers)。
- 原始型別有了固有方法（inherent method），不必再匯入 `SliceExt` 這類擴充特徵；`MyType::default()` 這種不寫特徵名稱的關聯路徑也能使用。
- crate 名稱不能再含 `-`；Cargo 會自動把套件名稱的 `-` 轉成 `_` 作為 crate 名稱，詳見 [Cargo](/modules-cargo)。
- 生命週期遮蔽（lifetime shadowing）改為編譯錯誤，詳見 [生命週期](/ownership-lifetimes)。
- `Send` 不再隱含 `'static`；`Send`、`Sync` 改由標準庫定義，詳見 [Send 與 Sync](/concurrency-send-sync)。
- 二進位與八進位字面值會一次讀完整串數字，例如 `0b1234` 被視為一個字面值，而不是 `0b1` 和 `234` 兩個。

### 標準庫重點

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

### Cargo 與工具

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

### 相容性注意

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

### 字串搜尋的 Pattern

`find`、`split` 這類字串搜尋方法接受的參數稱為 Pattern：可以是單一字元、字串切片，也可以是「判斷字元」的閉包或函式。同一個方法就能涵蓋「找某個字」「找某段字」「找符合條件的字」三種需求。

範例：

```rust
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);
}
```

輸出：

```text
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<目標型別>`，呼叫端就能直接傳入任何可轉換的值。

範例：

```rust
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);
}
```

輸出：

```text
212
-40°F
[102, 101, 114, 114, 105, 115]
```

### 以值索引 HashMap

`Index` 改為以值接收索引後，`HashMap<String, _>` 可以直接用字串切片 `map["key"]` 取值，寫法和陣列、`Vec` 的 `v[1]` 一致。

範例：

```rust
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]);
}
```

輸出：

```text
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](https://github.com/rust-lang/cargo/security/advisories/GHSA-p688-r7jv-fm6f) | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | [1.96.0](/rust-1-96) |
| [CVE-2026-5223](https://github.com/rust-lang/cargo/security/advisories/GHSA-jq42-7mfv-hm57) | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | [1.96.0](/rust-1-96) |
| [CVE-2024-43402](https://github.com/rust-lang/rust/security/advisories/GHSA-2xg3-7mm6-98jj) | std | `std::process::Command` batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | [1.81.0](/rust-1-81) |
| [CVE-2024-24576](https://github.com/rust-lang/rust/security/advisories/GHSA-q455-m56c-85mh) | std | `std::process::Command` did not properly escape arguments of batch files on Windows | 早於 1.77.2 | [1.77.2](/rust-1-77) |
| [CVE-2023-38497](https://github.com/rust-lang/cargo/security/advisories/GHSA-j3xp-wfr4-hx87) | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | [1.71.1](/rust-1-71) |
| [CVE-2022-46176](https://github.com/rust-lang/cargo/security/advisories/GHSA-r5w3-xm58-jv6j) | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | [1.66.1](/rust-1-66) |
| [CVE-2022-36113](https://github.com/rust-lang/cargo/security/advisories/GHSA-rfj2-q3h3-hm5j) | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | [1.64.0](/rust-1-64) |
| [CVE-2022-36114](https://github.com/rust-lang/cargo/security/advisories/GHSA-2hvr-h6gw-qrxp) | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | [1.64.0](/rust-1-64) |
| [CVE-2022-21658](https://github.com/rust-lang/rust/security/advisories/GHSA-r9cc-f5pr-p3j2) | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | [1.58.1](/rust-1-58) |
| [CVE-2021-42574](https://github.com/rust-lang/rust/security/advisories/GHSA-rcv6-wg5m-24v6) | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | [1.56.1](/rust-1-56) |
| [CVE-2021-29922](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-29922) | std | Improper Input Validation of octal literals in `std::net` | 早於 1.53.0 | [1.53.0](/rust-1-53) |
| [CVE-2019-16760](https://github.com/rust-lang/rust/security/advisories/GHSA-phjm-8x66-qw4r) | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | [1.26.0](/rust-1-26) |
| [CVE-2018-1000622](https://groups.google.com/forum/#!topic/rustlang-security-announcements/4ybxYLTtXuM) | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | [1.27.1](/rust-1-27) |
| [CVE-2017-20004](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-20004) | std | MutexGuard\<Cell\<i32\>\> must not be Sync | 早於 1.19.0 | [1.19.0](/rust-1-19) |
| [CVE-2015-20001](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-20001) | std | Panic safety violation in BinaryHeap | 早於 1.2.0 | [1.2.0](/rust-1-2) |

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

## 資料來源

- [Rust 1.0 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-100-2015-05-15)
- [Rust 1.0 發布公告](https://blog.rust-lang.org/releases/1.0.0/)
- [rust-lang/rust 原始碼 1.0.0](https://github.com/rust-lang/rust/tree/1.0.0)
