Rust 1.8
最後更新
Rust 1.8 發布於 2016-04-14,目前最新修補版為
1.8.0,已停止支援,請升級到 1.100.1。版本介紹
Rust 1.8 讓
+=、-= 這類複合指定運算子可以透過 AddAssign、SubAssign 等特徵多載,自訂型別也能寫 a += b;沒有欄位的結構也能用大括號 struct Foo {} 宣告。標準庫穩定約 20 個 API,包括表示時間點的 time::Instant 與 time::SystemTime、str::encode_utf16。升級要注意:32 位元 MSVC 建置支援 unwinding,i686-pc-windows-msvc 升為 tier 1 平台;新的不穩定編譯器旗標不再能在穩定版使用。語言變更
- 複合指定運算可透過
AddAssign、SubAssign、MulAssign、DivAssign、RemAssign、BitAndAssign、BitOrAssign、BitXorAssign、ShlAssign、ShrAssign多載,詳見 std::ops。 - 空結構可以寫成
struct Foo { },與struct Foo;並存,詳見 結構。
標準庫重點
time::Instant(單調遞增的時間點,量測經過時間用)與time::SystemTime(系統時鐘)穩定,附now、elapsed、duration_since與UNIX_EPOCH,詳見 std::time。str::encode_utf16(原名utf16_units)把字串編成 UTF-16 單元,詳見 &str。Ref::map、RefMut::map讓RefCell的借用只指向內部某一部分,詳見 std::cell。ptr::drop_in_place穩定,詳見 std::ptr。write!/writeln!在引數無法格式化時會正確回報錯誤,詳見 std::fmt。sync::mpsc的所有型別實作Debug,詳見 std::sync::mpsc。
Cargo 與工具
- 新增
cargo init,在目前目錄建立 Cargo 專案,不像cargo new會新建子目錄,詳見 Cargo。 - 新增
cargo metadata子命令,取得專案的 metadata。 .cargo/config的[term]區段可設定verbose與color;字串或整數設定能用環境變數覆寫,例如CARGO_BUILD_JOBS。- 平台專屬相依可用 Rust 的
cfg語法描述目標平台。 rustc --print cfg印出目前目標平台定義的cfg;非窮舉match的錯誤最多列出 3 個缺少的變體並告知總數。- 新的、以 Cargo 為基礎並用 Rust 撰寫的編譯器建置系統開始可用,預計取代 Makefile。
相容性注意
- Unix 的
MetadataExt不再回傳平台專屬型別,改回傳加寬的整數。 - 不能再用
Variant(..)比對沒有欄位的列舉變體(1.6 起為警告),詳見 match。 - 從檔案載入的模組只能出現在其他模組內,不能放在任意區塊中,詳見 模組系統。
- Unix 上堆疊溢位改為執行期中止(abort),不再是 SIGSEGV。
Command::spawn等函式在命令列引數含NUL字元時回傳錯誤,詳見 std::process。
多載 += 運算子
為型別實作
AddAssign,就能對它使用 +=;右邊的型別可以和左邊不同,例如下面的 MulAssign<i64> 讓金額乘上一個整數。方法參數是 &mut self,直接修改左邊的值,不會產生新值。範例:
- use std::ops::{AddAssign, MulAssign};
- #[derive(Debug, Clone, Copy)]
- struct Money {
- cents: i64,
- }
- impl AddAssign for Money {
- fn add_assign(&mut self, other: Money) {
- self.cents += other.cents;
- }
- }
- impl MulAssign<i64> for Money {
- fn mul_assign(&mut self, factor: i64) {
- self.cents *= factor;
- }
- }
- fn main() {
- let mut wallet = Money { cents: 1_000 };
- wallet += Money { cents: 250 };
- println!("{:?}", wallet);
- wallet *= 3;
- println!("{:?}", wallet);
- }
輸出:
- Money { cents: 1250 }
- Money { cents: 3750 }
大括號空結構與 encode_utf16
struct Marker {} 與 struct Unit; 都是沒有欄位的結構,建立時分別寫 Marker {} 與 Unit;開發中常在「有欄位」與「沒欄位」之間切換時,大括號寫法改起來比較順。encode_utf16 把字串轉成 UTF-16 編碼單元,和 Windows API 或 JavaScript 交換字串時會用到。範例:
- #[derive(Debug, Default)]
- struct Marker {}
- #[derive(Debug)]
- struct Unit;
- fn main() {
- let a = Marker {};
- let b = Marker::default();
- let c = Unit;
- println!("{:?} {:?} {:?}", a, b, c);
- let units: Vec<u16> = "Rust🦀".encode_utf16().collect();
- println!("{} 個 UTF-16 單元:{:x?}", units.len(), units);
- }
輸出:
- Marker Marker Unit
- 6 個 UTF-16 單元:[52, 75, 73, 74, d83e, dd80]
注意:BMP 以外的字元(例如 emoji)在 UTF-16 中佔兩個單元(代理對,surrogate pair),所以上例 5 個字元得到 6 個單元。
本版新增的棄用 API
以下 API 從 Rust 1.8 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std::os::darwin::fs | MetadataExt::as_raw_stat | deprecated in favor of the accessor methods of this trait |
std::os::linux::fs | MetadataExt::as_raw_stat | other methods of this trait are now preferred |
std::os::linux::raw | blkcnt_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | blksize_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | dev_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | ino_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | mode_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | nlink_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | off_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | pthread_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | stat | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::linux::raw | time_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | blkcnt_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | blksize_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | dev_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | gid_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | ino_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | mode_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | nlink_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | off_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | pid_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | pthread_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | time_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
std::os::unix::raw | uid_t | these type aliases are no longer supported by the standard library, the libc crate on crates.io should be used instead for the correct definitions |
修補版本
本版沒有修補版本。
已知漏洞
影響
1.8.0 的已知漏洞共 16 個;在本版修正的共 0 個;仍影響最新修補版 1.8.0 的共 16 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.8.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-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
| CVE-2019-16760 | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | 1.26.0 |
| CVE-2018-1000657 | std | Buffer overflow vulnerability in VecDeque::reserve() | 1.3.0 起,早於 1.22.0 | 1.22.0 |
| CVE-2018-1000622 | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | 1.27.1 |
| CVE-2018-25008 | std | Insufficient synchronization in Arc::get_mut | 1.3.0 起,早於 1.29.0 | 1.29.0 |
| CVE-2017-20004 | std | MutexGuard<Cell<i32>> must not be Sync | 早於 1.19.0 | 1.19.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。