# Rust 1.41

Rust 1.41 發布於 2020-01-30，目前最新修補版為 `1.41.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.41 放寬了孤兒規則（orphan rule），在泛型情境下可以替外部 trait 寫出以前被拒絕的實作，例如 `impl<T> From<MyType<T>> for Vec<T>`。Cargo 也有多項改進：`cargo install` 會更新過期的套件、`Cargo.lock` 改用較不易產生合併衝突的格式，並可以針對單一相依覆寫建置設定。升級時要注意：1.41 是最後一個以 tier 1 支援 32 位元 Apple 目標的版本。

### 語言變更

- 孤兒規則放寬：實作外部 trait 時可以傳入型別參數，例如 `impl<T> From<Foo> for Vec<T> {}`，詳見 [trait 基礎](/traits-basics)。
- `self` 的接收者型別可以任意巢狀，例如 `fn foo(self: Box<Box<Self>>)`。
- `format_args!` 中可以使用任何合法識別字，包含底線開頭的名稱。
- `Box<T>` 用在 `extern "C"` 函式時，C 端可以對應為 `T*`，詳見 [FFI](/lowlevel-ffi)。
- 可見性修飾詞（如 `pub`）在語法上可以出現在 trait 項目與列舉成員上，但語意上仍會被拒絕，主要供程序巨集與條件編譯解析。

### 標準庫重點

- `Result::map_or` 與 `Result::map_or_else` 穩定，與 `Option` 的同名方法對應，詳見 [Result](/pkg-result)。
- `rc::Weak` 與 `sync::Weak` 新增 `strong_count` 與 `weak_count`，詳見 [std::rc](/pkg-rc) 與 [std::sync](/pkg-sync)。
- 較小寬度的 `NonZero*` 可以用 `From` 轉成較大寬度，例如 `NonZeroU16` 實作 `From<NonZeroU8>`。
- `core::panic` 模組穩定；`MaybeUninit<T>` 實作 `fmt::Debug`。

### Cargo 與工具

- `cargo install` 偵測到套件過期時會重新安裝。
- `Cargo.lock` 改用對 git 更友善的格式，減少合併衝突。
- 可以覆寫特定相依的建置設定，例如 `[profile.dev.package.image] opt-level = 2`；`[profile.<profile>.build-override]` 覆寫建置腳本，詳見 [Cargo 專案](/modules-cargo)。
- 二進位 crate 的 `cargo doc` 預設會產生私有項目的文件。
- 文件中的程式碼區塊可以標 `edition2018` 指定 edition；`#[cfg(doc)]` 讓項目只在產生文件時編譯，詳見 [文件測試](/testing-doc)。
- `rustc` 會警告未使用的迴圈標籤；可以用 `@path` 傳入參數檔。

### 相容性注意

- 1.41 是最後一個以 tier 1 支援 32 位元 Apple 目標的版本，之後不再發佈這些平台的官方二進位檔。

### 放寬的孤兒規則

孤兒規則規定：實作 trait 時，trait 或型別至少要有一個屬於自己的 crate，避免兩個 crate 對同一組合寫出衝突的實作。以前只要牽涉泛型，`impl<T> From<Wrapper<T>> for Vec<T>` 這種「外部 trait、外部型別、但型別參數裡有自己的型別」的寫法會被拒絕；1.41 起編譯器能判斷它不會衝突，允許這個實作。

範例：

```rust
struct Wrapper<T>(Vec<T>);

impl<T> From<Wrapper<T>> for Vec<T> {
    fn from(w: Wrapper<T>) -> Vec<T> {
        w.0
    }
}

fn main() {
    let w = Wrapper(vec![1, 2, 3]);
    let v: Vec<i32> = w.into();
    println!("{:?}", v);
}
```

輸出：

```text
[1, 2, 3]
```

### Result::map_or 與 map_or_else

`map_or(預設值, 閉包)`：`Ok` 時用閉包轉換值，`Err` 時回傳預設值。`map_or_else(錯誤閉包, 成功閉包)` 則讓兩邊都用閉包處理，錯誤閉包可以讀取錯誤內容。注意參數順序：錯誤處理在前，成功處理在後。

範例：

```rust
fn main() {
    let ok: Result<i32, String> = Ok(4);
    let bad: Result<i32, String> = Err(String::from("壞掉"));

    println!("{}", ok.map_or(0, |n| n * 10));
    println!("{}", bad.clone().map_or(0, |n| n * 10));

    let text = bad.map_or_else(|e| format!("錯誤：{}", e), |n| n.to_string());
    println!("{}", text);
}
```

輸出：

```text
40
0
錯誤：壞掉
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.41.1` | 2020-02-27 | 否 | Always check types of static items；Always check lifetime bounds of `Copy` impls；Fix miscompilation in callers of `Layout::repeat`；Rust 1.41.0 was announced as the last Rust release with tier 1 or tier 2 support for 32-bit Apple targets. That announcement did not expect a patch release. 1.41.1 also includes release binaries for these targets. |

## 已知漏洞

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

### 仍影響 `1.41.1`

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [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-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-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.41 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1410-2020-01-30)
- [Rust 1.41 發布公告](https://blog.rust-lang.org/releases/1.41.0/)
- [rust-lang/rust 原始碼 1.41.0](https://github.com/rust-lang/rust/tree/1.41.0)
