# Rust 1.43

Rust 1.43 發布於 2020-04-23，目前最新修補版為 `1.43.1`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.43 是一個小版本，重點是巨集的 `item` 片段可以插入到 `trait`、`impl` 與 `extern` 區塊中，以及整數與浮點數的關聯常數可以直接使用：寫 `u32::MAX`、`f32::NAN` 不必再 `use std::u32;`。數值字面值搭配參考的型別推斷也修正了，Cargo 則為整合測試提供可執行檔路徑的環境變數。升級時要注意：`assert!` 後面多餘的語法從警告變成錯誤。

### 語言變更

- 巨集的 `item` 片段可以插入 `trait`、`impl` 與 `extern` 區塊，詳見 [宣告式巨集](/macros-declarative)。
- `#[cfg()]` 等屬性可以用在 `if` 運算式上。
- 修正 `&1.0` 這類對數字取參考後進行二元運算時的型別推斷。
- 幾項僅語法層面的變更（如 `type Foo: Ord`、所有 `fn` 中允許 `self`），語意上仍會被拒絕。

### 標準庫重點

- 整數與浮點數的關聯常數可以直接使用，例如 `u32::MAX`、`f32::NAN`，詳見 [整數](/types-integers) 與 [浮點數](/types-floats)。
- 新增 `primitive` 模組重新匯出原生型別，主要讓巨集避免型別名稱被遮蔽。
- `Once::is_completed` 與 `iter::once_with` 穩定，詳見 [std::sync](/pkg-sync) 與 [std::iter](/pkg-iter)。
- 新增常數 `f32::consts::LOG10_2`、`LOG2_10` 與 `f64` 的對應常數。
- `Arc<[T; N]>`、`Box<[T; N]>`、`Rc<[T; N]>` 實作從對應切片型別的 `TryFrom`（`N` 為 `0..=32`）。
- `String` 實作 `AsMut<str>`；`FromUtf8Error` 實作 `Clone` 與 `Eq`；`u8::is_ascii` 改為 `const fn`。

### Cargo 與工具

- `.cargo/config` 或環境變數可以設定 `[profile]`。
- 執行整合測試或基準測試時，Cargo 設定 `CARGO_BIN_EXE_<name>` 指向二進位檔的路徑，可以用 `env!("CARGO_BIN_EXE_my-program")` 取得，詳見 [整合測試](/testing-integration)。
- `rustc` 可以傳入多個 lint 旗標，後面的旗標覆寫前面的，例如 `-D unused -A unused-variables`。

### 相容性注意

- `assert!` 巨集後面有多餘語法改為硬錯誤，自 1.36 起它就是警告。
- 修正 `Self` 型別推斷錯誤，部分以前誤被接受的程式碼現在會編譯失敗。

### 直接使用數值型別的關聯常數

以前取得 `u32` 的最大值要寫 `std::u32::MAX`（模組常數）或先 `use std::u32;`。1.43 起 `MAX`、`MIN`、`EPSILON`、`NAN` 等都是型別本身的關聯常數，直接寫 `u32::MAX`、`f64::EPSILON` 即可。數學常數如 `LOG10_2` 仍放在 `std::f64::consts` 模組裡。

範例：

```rust
use std::f64::consts::LOG10_2;

fn main() {
    println!("{}", u32::MAX);
    println!("{}", i8::MIN);
    println!("{}", f64::EPSILON > 0.0);
    println!("{}", f32::NAN.is_nan());
    println!("{:.4}", LOG10_2);
}
```

輸出：

```text
4294967295
-128
true
true
0.3010
```

### 在 impl 中插入 item 片段

`$x:item` 片段比對一個完整的項目（函式、結構體等）。以前它只能放在模組層級，現在可以插進 `impl`、`trait` 與 `extern` 區塊，巨集能夠接收一個方法定義並放進產生的 `impl` 中。

範例：

```rust
macro_rules! with_greet {
    ($name:ident, $method:item) => {
        struct $name;
        impl $name {
            $method
        }
    };
}

with_greet!(Robot, fn greet(&self) -> &'static str { "beep" });

fn main() {
    println!("{}", Robot.greet());
}
```

輸出：

```text
beep
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `1.43.1` | 2020-05-07 | 是 | Updated openssl-src to 1.1.1g for CVE-2020-1967.；Fixed the stabilization of AVX-512 features.；Fixed `cargo package --list` not working with unpublished dependencies. |

## 已知漏洞

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

### 本版已修正

| 編號 | 元件 | 摘要（官方原文） | 影響範圍 | 修正於 |
| --- | --- | --- | --- | --- |
| [CVE-2020-1967](https://www.cve.org/CVERecord?id=CVE-2020-1967) | vendored | Updated openssl-src to 1.1.1g for CVE-2020-1967. | 未公布 | [1.43.1](/rust-1-43) |

### 仍影響 `1.43.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.43 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1430-2020-04-23)
- [Rust 1.43 發布公告](https://blog.rust-lang.org/releases/1.43.0/)
- [rust-lang/rust 原始碼 1.43.0](https://github.com/rust-lang/rust/tree/1.43.0)
