# Rust 1.79

Rust 1.79 發布於 2024-06-13，目前最新修補版為 `1.79.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.79 穩定了行內 `const { ... }` 運算式與「關聯型別位置的約束」（`T: Iterator<Item: Display>`），讓常見的泛型與常數寫法都變短。對新手來說，`[const { Vec::new() }; 3]` 這類以前要另外宣告常數的寫法現在一行就能完成；`if`／`match` 中的暫存值也比照區塊享有生命週期延長。升級時要注意：`RustcEncodable`／`RustcDecodable` 進入軟性棄用，最低外部 LLVM 版本升到 17。

### 語言變更

- 穩定行內 `const {}` 運算式，可以使用作用域中的泛型並自動推斷型別，詳見 [常數](/basics-constants)。
- 穩定關聯型別約束語法 `T: Trait<Assoc: Bound>`，詳見 [特徵約束](/traits-bounds)。
- 暫存值的生命週期延長規則擴及 `if` 與 `match` 運算式，詳見 [生命週期](/ownership-lifetimes)。
- 可以從其他模組或 crate 匯入 `main`。
- 新增 `redundant_lifetimes` 與 `unnameable_types` lint。

### 標準庫重點

- 新增泛型的 `num::NonZero<T>`，詳見 [std::num](/pkg-num)。
- 整數新增 `unchecked_add`、`unchecked_sub`、`unchecked_mul`；切片新增 `split_at_unchecked`。
- 新增 `<[u8]>::utf8_chunks`，把位元組依合法與不合法的 UTF-8 片段切開，詳見 [str](/pkg-str)。
- 新增 `path::absolute`，詳見 [std::path](/pkg-path)；新增 `io::Error::downcast`、`CStr::count_bytes`。
- 標準庫以 `-Cforce-frame-pointers=yes` 編譯，效能分析工具更容易取得呼叫堆疊。

### Cargo 與工具

- `cargo add` 會依專案的 `rust-version` 挑選相容的相依版本，詳見 [Cargo 與套件](/modules-cargo)。
- `lib.name` 中的連字號一律正規化為底線。
- rustc 穩定 `--check-cfg`，在編譯期檢查 `cfg` 名稱與值（Cargo 端在 1.80 啟用）。

### 相容性注意

- `RustcEncodable` 與 `RustcDecodable` 軟性棄用，會在下一個 edition 從 prelude 移除。
- `wasm_c_abi` 未來相容性 lint 會對不符規格的 C ABI 發出警告，`wasm-bindgen` 請用 0.2.88 以上。
- 最低外部 LLVM 版本升到 17。

### 行內 const 運算式

`const { ... }` 讓你在運算式中直接進入常數求值情境。最常見的用途是重複一個沒有實作 `Copy` 的值：`[Vec::new(); 3]` 無法編譯，但 `[const { Vec::new() }; 3]` 可以，因為每個元素都是同一個常數的新副本。

範例：

```rust
fn main() {
    let empty: [Vec<String>; 3] = [const { Vec::new() }; 3];
    println!("{:?}", empty);

    const LIMIT: u32 = 10;
    let doubled = const { LIMIT * 2 };
    println!("{}", doubled);
}
```

輸出：

```text
[[], [], []]
20
```

### 關聯型別位置的約束

以前要限制「迭代器的元素可以顯示」，必須多寫一個泛型參數或 `where I::Item: Display`。現在可以直接在 `Item` 後面加約束，意思與拆成兩條 `where` 子句相同。

範例：

```rust
use std::fmt::Display;

fn join_all<I>(items: I) -> String
where
    I: IntoIterator<Item: Display>,
{
    items
        .into_iter()
        .map(|x| x.to_string())
        .collect::<Vec<_>>()
        .join(", ")
}

fn main() {
    println!("{}", join_all([1, 2, 3]));
    println!("{}", join_all(vec!["a", "b"]));
}
```

輸出：

```text
1, 2, 3
a, b
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

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

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

## 資料來源

- [Rust 1.79 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1790-2024-06-13)
- [Rust 1.79 發布公告](https://blog.rust-lang.org/releases/1.79.0/)
- [rust-lang/rust 原始碼 1.79.0](https://github.com/rust-lang/rust/tree/1.79.0)
