# Rust 1.78

Rust 1.78 發布於 2024-05-02，目前最新修補版為 `1.78.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.78 穩定了 `#[diagnostic]` 屬性命名空間，函式庫作者可以用 `#[diagnostic::on_unimplemented]` 自訂「型別沒有實作某特徵」時的錯誤訊息。對新手來說，這代表用到設計良好的函式庫時會看到更易懂的編譯錯誤；同時開啟除錯斷言（debug build）時，標準庫會在更多 `unsafe` 函式的前置條件上檢查，及早抓出未定義行為。升級時要注意：Windows tier 1 目標至少需要 Windows 10，`wasm32-wasi` 開始改名為 `wasm32-wasip1`。

### 語言變更

- 穩定 `#[diagnostic]` 命名空間與 `#[diagnostic::on_unimplemented]` 屬性，詳見 [特徵進階](/traits-advanced)。
- 對 NaN 做模式比對改為硬錯誤，詳見 [浮點數](/types-floats)。
- 穩定 `#[cfg(target_abi = ...)]`。
- 新增 `non_contiguous_range_endpoints` lint，提醒排除區間模式後漏掉單一值的情況。
- `trait Trait: Auto {}` 可以把 `dyn Trait` 向上轉成 `dyn Trait + Auto`。

### 標準庫重點

- 開啟除錯斷言時，許多 `unsafe` 函式的前置條件會在執行期檢查，詳見 [unsafe](/lowlevel-unsafe)。
- `align_offset` 與 `align_to` 在所有情況下都有確定的行為，詳見 [std::ptr](/pkg-ptr)。
- `&Stdin` 實作 `Read`；`io::Error` 實作 `From<TryReserveError>`，詳見 [std::io](/pkg-io)。
- Unicode 更新到 15.1.0；pthread 版本的 `RwLock` 改用自家實作。

### Cargo 與工具

- 穩定 lockfile v4，產生 lockfile 時會參考 `rust-version`，詳見 [Cargo 與套件](/modules-cargo)。
- 官方支援 `target.<triple>.rustdocflags` 設定。
- 編譯器更新到 LLVM 18。

### 相容性注意

- Windows tier 1 目標至少需要 Windows 10。
- 新增 `wasm32-wasip1` 目標，`wasm32-wasi-preview1-threads` 改名為 `wasm32-wasip1-threads`。
- `INVALID_DOC_ATTRIBUTES` lint 改為預設拒絕。
- 修正 `const` 初始化中內部可變性的偵測，部分原本被誤接受的泛型程式碼不再能編譯。

### 自訂未實作特徵的錯誤訊息

在特徵上加 `#[diagnostic::on_unimplemented]`，就能在使用者傳錯型別時顯示自己的 `message`、`label` 與 `note`，`{Self}` 會被換成實際的型別。下面這段無法編譯：`42` 沒有實作 `Storable`，錯誤訊息改用特徵作者寫的文字。

範例：

```rust
#[diagnostic::on_unimplemented(
    message = "`{Self}` 不能存進背包",
    label = "這個型別沒有實作 Storable",
    note = "替 `{Self}` 實作 `Storable` 特徵後再試一次"
)]
trait Storable {}

struct Sword;
impl Storable for Sword {}

fn store<T: Storable>(_item: T) {}

fn main() {
    store(Sword);
    store(42);
}
```

輸出：

```text
error[E0277]: `{integer}` 不能存進背包
  --> main.rs:15:11
   |
15 |     store(42);
   |     ----- ^^ 這個型別沒有實作 Storable
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Storable` is not implemented for `{integer}`
   = note: 替 `{integer}` 實作 `Storable` 特徵後再試一次
```

注意：`#[diagnostic]` 只是給編譯器的提示，編譯器不一定採用；寫了編譯器不認得的診斷屬性也不算錯誤。錯誤訊息的排版細節之後仍可能調整，行號會依你的檔案而異。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.78.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.78 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1780-2024-05-02)
- [Rust 1.78 發布公告](https://blog.rust-lang.org/releases/1.78.0/)
- [rust-lang/rust 原始碼 1.78.0](https://github.com/rust-lang/rust/tree/1.78.0)
