# Rust 1.68

Rust 1.68 發布於 2023-03-09，目前最新修補版為 `1.68.2`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.68 穩定了 Cargo 的 sparse registry 協定：讀取 crates.io 索引時只下載實際用到的 crate 資訊，不必再 clone 整個索引倉庫，更新相依明顯更快（本版需手動開啟，1.70 起成為預設）。標準庫新增 `pin!` 巨集，在區域變數上建立 `Pin<&mut T>`，是自己輪詢（poll）future 時的必備工具。升級要注意：`std::task::Context` 不再實作 `Send` 與 `Sync`，Android 只支援 NDK 25 以上。

### 語言變更

- 只用 `alloc`、不用 `std` 的程式不必再自行定義記憶體配置失敗的處理函式；預設行為是 panic。含 `std` 的程式維持印出訊息後中止。
- `efiapi` 呼叫慣例穩定。
- 帶有 drop 邏輯的型別不再被隱式提升（promotion）為常數。

### 標準庫重點

- `core::pin::pin!` 與 `std::pin::pin!` 穩定，詳見 [std::pin](/pkg-pin)。
- `f32`、`f64` 實作 `From<bool>`，`true` 轉成 `1.0`、`false` 轉成 `0.0`，轉換見 [std::convert](/pkg-convert)。
- 新增 `std::path::MAIN_SEPARATOR_STR`，以 `&str` 提供平台的路徑分隔符；`PathBuf` 實作 `DerefMut`，詳見 [std::path](/pkg-path)。
- `VecDeque::new` 可在 const 環境使用；`Vec` 轉 `VecDeque` 保證是 O(1)，詳見 [std::collections](/pkg-collections)。

### Cargo 與工具

- crates.io 可以使用 sparse 協定：設定環境變數 `CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse`，或在 `.cargo/config.toml` 加入 `[registries.crates-io]` 的 `protocol = "sparse"`，詳見 [Cargo](/modules-cargo)。
- `cargo build --verbose` 會說明為什麼需要重新編譯。
- `bindings_with_variant_name` lint 改為預設拒絕。

### 相容性注意

- `std::task::Context` 不再是 `Send`、`Sync`，詳見 [std::task](/pkg-task)。
- 只支援 Android NDK 25 或更新版本。

### pin!：在區域變數上固定 future

要手動呼叫 `Future::poll`，必須先把 future 固定（pin）成 `Pin<&mut F>`，保證它在記憶體中不會再被移動。以前要用 `Box::pin` 配置到堆積上，或借助第三方巨集；現在 `pin!(fut)` 直接在當前作用域固定，借用檢查器會確保這個 `Pin` 不會活得比 future 更久。固定的觀念見 [async 基礎](/async-basics)。

範例中的 `Waker::noop` 是 1.85 才穩定的 API，用來提供一個什麼都不做的喚醒器，讓範例不需要非同步執行環境。

範例：

```rust
use std::future::Future;
use std::pin::pin;
use std::task::{Context, Poll, Waker};

async fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let mut fut = pin!(add(2, 3));
    let mut cx = Context::from_waker(Waker::noop());
    match fut.as_mut().poll(&mut cx) {
        Poll::Ready(v) => println!("完成：{v}"),
        Poll::Pending => println!("尚未完成"),
    }
}
```

輸出：

```text
完成：5
```

### From<bool> for f64 與 MAIN_SEPARATOR_STR

`f64::from(true)` 得到 `1.0`，把布林陣列加總後除以長度，就是通過率。`MAIN_SEPARATOR_STR` 是 `&str` 版本的路徑分隔符，可以直接傳給 `join`；`PathBuf` 實作 `DerefMut` 後，也能把它當成可變的 `Path` 使用。

範例：

```rust
use std::path::{MAIN_SEPARATOR_STR, PathBuf};

fn main() {
    let passed = [true, false, true, true];
    let rate: f64 = passed.iter().map(|&b| f64::from(b)).sum::<f64>() / passed.len() as f64;
    println!("{rate}");

    let parts = ["usr", "local", "bin"];
    println!("{}", parts.join(MAIN_SEPARATOR_STR));

    let mut p = PathBuf::from("logs");
    p.push("app.log");
    println!("{}", p.display());
}
```

輸出：

```text
0.75
usr/local/bin
logs/app.log
```

注意：路徑分隔符依平台而定，以上是在 Unix 系統的輸出；Windows 上會是 `\`。

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.68.1` | 2023-03-23 | 否 | Fix miscompilation in produced Windows MSVC artifacts This was introduced by enabling ThinLTO for the distributed rustc which led to miscompilations in the resulting binary. Currently this is believed to be limited to the -Zdylib-lto flag used for rustc compilation, rather than a general bug in ThinLTO, so only rustc artifacts should be affected.；Fix --enable-local-rust builds；Treat `$prefix-clang` as `clang` in linker detection code；Fix panic in compiler code |
| `1.68.2` | 2023-03-28 | 否 | Update the GitHub RSA host key bundled within Cargo. The key was rotated by GitHub on 2023-03-24 after the old one leaked.；Mark the old GitHub RSA host key as revoked. This will prevent Cargo from accepting the leaked key even when trusted by the system.；Add support for `@revoked` and a better error message for `@cert-authority` in Cargo's SSH host key verification |

## 已知漏洞

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

### 仍影響 `1.68.2`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [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-40030](https://github.com/rust-lang/cargo/security/advisories/GHSA-wrrj-h57r-vx9p) | cargo | Malicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports | 1.60.0 起，早於 1.72.0 | [1.72.0](/rust-1-72) |
| [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) |

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

## 資料來源

- [Rust 1.68 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1680-2023-03-09)
- [Rust 1.68 發布公告](https://blog.rust-lang.org/releases/1.68.0/)
- [rust-lang/rust 原始碼 1.68.0](https://github.com/rust-lang/rust/tree/1.68.0)
