# Rust 1.48

Rust 1.48 發布於 2020-11-19，目前最新修補版為 `1.48.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.48 的主角是文件工具 rustdoc：文件註解裡可以直接用項目路徑建立連結（intra-doc links），也能用 `#[doc(alias)]` 加入搜尋別名。標準庫方面，任意長度的陣列都實作了 `TryFrom<Vec<T>>`，可以把長度剛好的 `Vec` 轉成固定長度陣列。對新手來說，寫文件互相連結不必再手寫相對網址；升級時要注意 `const fn` 中參考提升為 `'static` 的規則變得和一般函式一致。

### 語言變更

- `unsafe` 關鍵字在語法上可以寫在模組前面；語意上仍會被拒絕，但程序式巨集可以解析它。

### 標準庫重點

- 任意長度的陣列實作 `TryFrom<Vec<T>>`，詳見 [型別轉換](/types-conversion) 與 [陣列](/pkg-array)。
- 新增 `VecDeque::make_contiguous`，詳見 [集合](/pkg-collections)。
- 新增 `future::ready`、`future::pending`，詳見 [std::future](/pkg-future)。
- 新增 `slice::as_ptr_range`、`slice::as_mut_ptr_range`，詳見 [切片](/pkg-slice)。
- `Option::is_some`、`Option::as_ref`、`Result::is_ok`、`Ordering::reverse` 等方法成為 `const fn`。
- `matches!` 巨集允許結尾逗號；`Vec<A>` 實作 `PartialEq<[B]>`。

### Cargo 與工具

- rustdoc 支援 intra-doc links，例如 ``/// [`crate::foo::Foo`]`` 會自動連到該型別，詳見 [註解與文件](/basics-comments-docs)。
- 項目可以加上 `#[doc(alias = "...")]`，在 rustdoc 搜尋時用別名找到。
- 穩定 `-C link-self-contained=<yes|no>`；`linux-gnu` 目標可以使用 `-C target-feature=+crt-static`。

### 相容性注意

- `const fn` 中的參考提升為 `'static` 的規則改成和一般函式相同，`&foo()` 在 `const fn` 中不再被提升。
- `mem::uninitialized` 用在不允許全零初始化的型別上會 panic。
- `#[link_name]`、`#[cold]` 等屬性放在沒有作用的位置會產生警告；`#[target_feature]` 放錯位置會編譯錯誤。
- 外部例外被 `catch_unwind` 捕捉時會中止程式。

### 從 Vec 轉成固定長度陣列

`<[T; N]>::try_from(vec)` 在長度剛好等於 `N` 時成功，得到陣列；長度不符時回傳 `Err`，裡面就是原本的 `Vec`，資料不會遺失。範例直接使用 `try_from`：1.48 當時需要先 `use std::convert::TryFrom`，2021 版次（edition）起 `TryFrom` 已在 prelude 中。

範例：

```rust
fn main() {
    let v = vec![10, 20, 30];
    let arr: [i32; 3] = <[i32; 3]>::try_from(v).unwrap();
    println!("{:?}", arr);

    let wrong = vec![1, 2, 3, 4, 5];
    match <[i32; 3]>::try_from(wrong) {
        Ok(a) => println!("ok {:?}", a),
        Err(back) => println!("長度不符，原本的 Vec 還給你：{:?}", back),
    }
}
```

輸出：

```text
[10, 20, 30]
長度不符，原本的 Vec 還給你：[1, 2, 3, 4, 5]
```

### VecDeque::make_contiguous

`VecDeque` 是環狀緩衝區，資料可能分成前後兩段，所以 `as_slices()` 回傳兩個切片。`make_contiguous()` 會把資料重新排成連續的一段並回傳 `&mut [T]`，之後就能直接用切片的排序等方法。

範例：

```rust
use std::collections::VecDeque;

fn main() {
    let mut q = VecDeque::new();
    q.push_back(3);
    q.push_back(4);
    q.push_front(2);
    q.push_front(1);

    let slice: &mut [i32] = q.make_contiguous();
    slice.sort_by(|a, b| b.cmp(a));
    println!("{:?}", slice);
    println!("{:?}", q.as_slices());
}
```

輸出：

```text
[4, 3, 2, 1]
([4, 3, 2, 1], [])
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.48.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-31162](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-31162) | std | Double free in Vec::from_iter specialization when drop panics | 1.48.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28878](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28878) | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28879](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28879) | std | Zip can cause buffer overflow when a consumed Zip iterator is used again | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28876](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28876) | std | Panic safety issue in Zip specialization | 1.14.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2021-28875](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28875) | std | Logic bug in Read can cause buffer overflow in read_to_end() | 1.20.0 起，早於 1.50.0 | [1.50.0](/rust-1-50) |
| [CVE-2021-28877](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-28877) | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起，早於 1.51.0 | [1.51.0](/rust-1-51) |
| [CVE-2020-36323](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-36323) | std | API soundness issue in join() implementation of \[Borrow\<str\>\] | 1.28.0 起，早於 1.52.0 | [1.52.0](/rust-1-52) |
| [CVE-2020-36318](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-36318) | std | VecDeque::make_contiguous may duplicate the contained elements | 1.48.0 起，早於 1.49.0 | [1.49.0](/rust-1-49) |
| [CVE-2020-36317](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-36317) | std | String::retain allows safely creating invalid strings when abusing panic | 1.26.0 起，早於 1.49.0 | [1.49.0](/rust-1-49) |
| [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) |

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

## 資料來源

- [Rust 1.48 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1480-2020-11-19)
- [Rust 1.48 發布公告](https://blog.rust-lang.org/releases/1.48.0/)
- [rust-lang/rust 原始碼 1.48.0](https://github.com/rust-lang/rust/tree/1.48.0)
