# Rust 1.38

Rust 1.38 發布於 2019-09-26，目前最新修補版為 `1.38.0`，**已停止支援**，請升級到 `1.100.1`。

## 版本介紹

Rust 1.38 的重點是管線化編譯（pipelined compilation）：相依的 crate 只要完成型別資訊與 metadata，下游 crate 就能開始編譯，不必等上游的程式碼產生結束，多 crate 專案的建置時間在部分情況下明顯縮短，而且不需要任何設定。同一版也新增了 `std::any::type_name`，並讓 `rustc` 對已知會造成未定義行為的 `mem::uninitialized`、`mem::zeroed` 用法發出警告。

### 語言變更

- `#[global_allocator]` 可以用在子模組中。
- `#[deprecated]` 可以標在巨集上。

### 標準庫重點

- `any::type_name` 取得型別名稱的字串，主要用於除錯，詳見 [std::any](/pkg-any)。
- `Duration` 新增 `as_secs_f32`、`as_secs_f64`、`from_secs_f32`、`from_secs_f64`、`mul_f32`、`mul_f64`、`div_f32`、`div_f64`，以浮點數表示或運算時間，詳見 [std::time](/pkg-time)。
- 所有數值型別新增歐幾里得除法 `div_euclid` 與餘數 `rem_euclid`，整數另有 `checked`、`overflowing`、`wrapping` 版本，詳見 [數值](/pkg-num)。
- 原始指標新增 `<*const T>::cast` 與 `<*mut T>::cast`，詳見 [std::ptr](/pkg-ptr)。
- `Clone`、`Debug` 等 prelude trait 的 derive 巨集可以用和 trait 相同的路徑取得，例如 `std::clone::Clone`；內建巨集也都能從 `std` 根取得，例如 `std::include_bytes!`。
- `Arc<[T]>` 與 `Rc<[T]>` 實作 `FromIterator<T>`；`iter::StepBy`、`Peekable`、`Take` 實作 `DoubleEndedIterator`。
- `slice::concat`、`join` 可以接受 `&[T]` 作為分隔或元素。

### Cargo 與工具

- Cargo 支援管線化編譯。
- `--features` 可以重複傳入多次來啟用多個 feature。
- rustdoc：`pub use` 上的文件會加在被重新匯出項目的文件前面。

### 相容性注意

- `rustc` 1.38.0 無法建置 `x86_64-unknown-uefi` 平台；`armv7-unknown-linux-gnueabihf` 平台與 `libc` 等部分 crate 已知有問題。

### std::any::type_name

`type_name::<T>()` 回傳型別名稱的字串，搭配泛型可以看出編譯器推斷出的實際型別，對理解泛型與型別推斷很有幫助。官方文件說明輸出格式不保證穩定，只能用於除錯，不要拿來比對或當作識別碼。範例的 `Vec<u8>` 印成 `alloc::vec::Vec<u8>`，顯示出它實際定義在 `alloc` crate。

範例：

```rust
use std::any::type_name;

fn name_of<T>(_: &T) -> &'static str {
    type_name::<T>()
}

fn main() {
    println!("{}", type_name::<i32>());
    println!("{}", name_of(&vec![1u8]));
    println!("{}", name_of(&"hi"));
}
```

輸出：

```text
i32
alloc::vec::Vec<u8>
&str
```

### Duration 的浮點運算與歐幾里得除法

`Duration::from_secs_f64(1.5)` 直接用秒數建立時間長度，`mul_f64`、`div_f32` 讓「乘以倍率」不必先換算成奈秒。歐幾里得除法則處理負數：一般的 `%` 結果和被除數同號，`-7 % 3` 是 `-1`；`rem_euclid` 永遠回傳非負數，`-7` 對 `3` 取餘得 `2`，計算循環索引時特別好用。

範例：

```rust
use std::time::Duration;

fn main() {
    let d = Duration::from_secs_f64(1.5);
    println!("{:?}", d);
    println!("{}", d.as_secs_f64());
    println!("{:?}", d.mul_f64(2.0));
    println!("{:?}", d.div_f32(4.0));

    println!("{} {}", -7 % 3, (-7i32).rem_euclid(3));
    println!("{} {}", -7 / 3, (-7i32).div_euclid(3));
}
```

輸出：

```text
1.5s
1.5
3s
375ms
-1 2
-2 -3
```

注意：`Duration::from_secs_f64` 傳入負數、NaN 或超出範圍的值會 panic。

## 本版新增的棄用 API

以下 API 從 Rust 1.38 起標記為棄用（deprecated），新程式不要再使用；說明欄為官方原文。

| 模組 | 項目 | 官方說明 |
| --- | --- | --- |
| `std::sync` | [`ONCE_INIT`](https://doc.rust-lang.org/std/sync/constant.ONCE_INIT.html) | the Once::new() function is now preferred |

## 修補版本

本版沒有修補版本。

## 已知漏洞

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

### 仍影響 `1.38.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-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.38 Release Notes](https://doc.rust-lang.org/stable/releases.html#version-1380-2019-09-26)
- [Rust 1.38 發布公告](https://blog.rust-lang.org/releases/1.38.0/)
- [rust-lang/rust 原始碼 1.38.0](https://github.com/rust-lang/rust/tree/1.38.0)
