# Rust 1.83

Rust 1.83 發布於 2024-11-28，目前最新修補版為 `1.83.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.83 大幅擴充常數求值（const）能做的事：`const fn` 裡可以使用 `&mut` 可變參考，常數初始化時也能參考 `static`，並有一大批既有 API 變成可以在 `const` 情境使用。對新手來說，更多計算可以搬到編譯期完成；標準庫也新增 `Option::get_or_insert_default`、`Entry::insert_entry` 以及十多個 `io::ErrorKind` 變體。升級時要注意：`#![cfg_attr(..., crate_type = ...)]` 這類寫法改為硬錯誤，且不再支援 Visual Studio 2013。

### 語言變更

- `const` 情境中可以使用 `&mut`、`*mut`、`&Cell` 與 `*const Cell`，詳見 [常數](/basics-constants)。
- `const` 初始化中可以建立指向 `static` 的參考。
- 新增原始生命週期與標籤語法 `'r#ident`。
- `expr_2021` 巨集片段指定符在所有 edition 都可使用，詳見 [宣告式巨集](/macros-declarative)。
- `#[non_exhaustive]` 結構可以沒有任何欄位；`non_local_definitions` lint 改為預設警告。

### 標準庫重點

- 新增 `Option::get_or_insert_default`，詳見 [Option](/pkg-option)。
- 新增 `hash_map::Entry::insert_entry` 與 `VacantEntry::insert_entry`，詳見 [HashMap](/pkg-collections-hash-map)。
- `io::ErrorKind` 新增 `NotADirectory`、`IsADirectory`、`DirectoryNotEmpty`、`StorageFull`、`ReadOnlyFilesystem` 等變體，詳見 [std::io](/pkg-io)。
- 新增 `BufRead::skip_until`、`ControlFlow::break_value` 等方法、`Debug*::finish_non_exhaustive`、`char::MIN`、`Waker::new`。
- `Option::unwrap`、`mem::replace`、浮點數的 `to_bits`／`from_bits` 等大量 API 可以在 `const` 中使用。
- Unicode 更新到 16.0.0；`ExitCode` 實作 `PartialEq`。

### Cargo 與工具

- 新增 `CARGO_MANIFEST_PATH` 環境變數，直接指向 `Cargo.toml` 檔案，詳見 [Cargo 與套件](/modules-cargo)。
- `Cargo.toml` 新增 `package.autolib`，可以關閉 `[lib]` 自動偵測。
- rustdoc 側邊欄的目錄會列出項目文件中的 `#` 標題。

### 相容性注意

- `deprecated_cfg_attr_crate_type_name` 改為硬錯誤：不能在 `#![cfg_attr]` 中設定 `crate_type`／`crate_name`，請改用命令列參數。
- 安裝 `rust-src` 時，診斷與回溯中的標準庫路徑會換成本機實際路徑，而不是 `/rustc/<hash>`。
- 對非 `struct`／`enum`／`union` 項目寫 `repr(Rust)` 會報錯；最低外部 LLVM 版本升到 18。
- MSVC 目標不再支援 Visual Studio 2013。

### const fn 中的可變參考

以前 `const fn` 只能讀不能改：不能接收 `&mut` 參數。現在可以把可變參考傳進輔助函式，在編譯期一步步累加、修改，最後的結果直接寫進常數。

範例：

```rust
const fn bump(counter: &mut u32, step: u32) {
    *counter += step;
}

const fn total() -> u32 {
    let mut n = 0;
    bump(&mut n, 3);
    bump(&mut n, 4);
    n
}

const TOTAL: u32 = total();

fn main() {
    println!("{TOTAL}");
}
```

輸出：

```text
7
```

注意：常數的最終值仍不能包含可變參考；`&mut` 只能在求值過程中暫時使用。

### get_or_insert_default 與 insert_entry

`Option::get_or_insert_default` 在值是 `None` 時先放入型別的預設值，再回傳可變參考，適合「第一次用到才建立清單」。`Entry::insert_entry` 寫入值後回傳 `OccupiedEntry`，可以接著讀取鍵與值。

範例：

```rust
use std::collections::HashMap;

fn main() {
    let mut tags: Option<Vec<&str>> = None;
    tags.get_or_insert_default().push("rust");
    tags.get_or_insert_default().push("新手");
    println!("{:?}", tags);

    let mut stock: HashMap<&str, u32> = HashMap::new();
    let entry = stock.entry("apple").insert_entry(5);
    println!("{} = {}", entry.key(), entry.get());
}
```

輸出：

```text
Some(["rust", "新手"])
apple = 5
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

影響 `1.83.0` 的已知漏洞共 2 個；在本版修正的共 0 個；仍影響最新修補版 `1.83.0` 的共 2 個。官方未公布影響範圍的漏洞只列在修正它的版本，不計入其他版本。

### 仍影響 `1.83.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) |

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

## 資料來源

- [Rust 1.83 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1830-2024-11-28)
- [Rust 1.83 發布公告](https://blog.rust-lang.org/releases/1.83.0/)
- [rust-lang/rust 原始碼 1.83.0](https://github.com/rust-lang/rust/tree/1.83.0)
