Rust 1.37
最後更新
Rust 1.37 發布於 2019-08-15,目前最新修補版為
1.37.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.37 讓列舉成員可以透過型別別名存取,並新增名稱為
_ 的匿名常數,方便巨集產生不會撞名的項目。Cargo 內建了 cargo vendor,可以把所有相依的原始碼下載到專案的 vendor/ 目錄;rustc 也支援以 -C profile-generate、-C profile-use 進行剖析導引最佳化(PGO)。升級時要注意:範圍模式使用 ... 與 trait 物件省略 dyn 從這版起預設產生警告。語言變更
- 可以透過型別別名使用列舉成員,例如
type MyOption = Option<u8>;之後寫MyOption::Some(y),詳見 列舉。 - 常數可以用
_命名,例如const _: u32 = 5;,同一個作用域可以有多個。 #[repr(align(N))]可以用在列舉上。cfg與cfg_attr可以用在泛型參數上。#[must_use]型別被包在元組、Box或陣列中而未使用時也會警告。- 巨集的
?重複運算子在 2015 edition 也能使用。
標準庫重點
Option::xor:兩者恰好一個是Some時回傳它,否則回傳None,詳見 Option。slice::copy_within在同一個切片內複製一段元素,詳見 切片。- 所有整數與
Wrapping新增reverse_bits,反轉位元順序。 BufReader::buffer、BufWriter::buffer查看緩衝區內容,詳見 std::io。Cell::from_mut、Cell<[T]>::as_slice_of_cells與DoubleEndedIterator::nth_back穩定。mem::MaybeUninit<T>與T的 ABI 相容。
Cargo 與工具
- 內建
cargo vendor,把相依下載到vendor/並印出建置時使用它們的設定。 - 在
[package]寫default-run = "foo"可以指定cargo run預設執行的二進位檔,詳見 Cargo 專案。 - 發佈含可執行檔的 crate 時,預設會一併包含
Cargo.lock。
相容性注意
- 包含範圍模式使用
...會預設警告,請改用..=。 - trait 物件沒有寫
dyn會預設警告,請改寫成dyn Trait,詳見 trait 物件。
透過型別別名使用列舉成員
為很長的泛型型別取別名很常見,例如
type Reply = Result<u32, String>;。以前別名只能用在型別位置,建構或比對成員時仍要寫回 Result::Ok;現在可以直接寫 Reply::Ok、Reply::Err,讓程式碼與別名一致。範例:
- type Reply = Result<u32, String>;
- fn show(r: Reply) -> String {
- match r {
- Reply::Ok(n) => format!("成功 {}", n),
- Reply::Err(e) => format!("失敗 {}", e),
- }
- }
- fn main() {
- println!("{}", show(Reply::Ok(7)));
- println!("{}", show(Reply::Err(String::from("timeout"))));
- }
輸出:
- 成功 7
- 失敗 timeout
匿名常數與其他新 API
const _ 宣告一個不需要名字的常數,常見用途是在編譯期做檢查:範例中如果 u64 不是 8 個位元組,程式根本無法編譯。範例也示範 Option::xor、copy_within(把索引 0..2 複製到索引 3 開始的位置)與 reverse_bits。範例:
- const _: () = assert!(std::mem::size_of::<u64>() == 8);
- fn main() {
- println!("{:?}", Some(1).xor(None::<i32>));
- println!("{:?}", Some(1).xor(Some(2)));
- let mut buf = [1, 2, 3, 4, 5];
- buf.copy_within(0..2, 3);
- println!("{:?}", buf);
- println!("{:08b}", 0b0000_0110u8.reverse_bits());
- }
輸出:
- Some(1)
- None
- [1, 2, 3, 1, 2]
- 01100000
注意:範例的
assert! 寫在常數裡,需要 Rust 1.57 起支援的 const panic;1.37 推出時的匿名常數主要搭配巨集使用。本版新增的棄用 API
以下 API 從 Rust 1.37 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std::os::unix::process | CommandExt::before_exec | should be unsafe, use pre_exec instead |
修補版本
本版沒有修補版本。
已知漏洞
影響
1.37.0 的已知漏洞共 18 個;在本版修正的共 0 個;仍影響最新修補版 1.37.0 的共 18 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.37.0
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| CVE-2026-5222 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
| CVE-2024-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
| CVE-2023-38497 | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | 1.71.1 |
| CVE-2022-46176 | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | 1.66.1 |
| CVE-2022-36113 | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | 1.64.0 |
| CVE-2022-36114 | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | 1.64.0 |
| CVE-2022-21658 | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | 1.58.1 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-28878 | std | Zip may call __iterator_get_unchecked twice with the same index | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| 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 |
| CVE-2021-28876 | std | Panic safety issue in Zip specialization | 1.14.0 起,早於 1.52.0 | 1.52.0 |
| 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 |
| CVE-2021-28877 | std | TrustedRandomAccess specialization composes incorrectly for nested iter::Zips | 1.11.0 起,早於 1.51.0 | 1.51.0 |
| CVE-2020-36323 | std | API soundness issue in join() implementation of [Borrow<str>] | 1.28.0 起,早於 1.52.0 | 1.52.0 |
| CVE-2020-36317 | std | String::retain allows safely creating invalid strings when abusing panic | 1.26.0 起,早於 1.49.0 | 1.49.0 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。