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,直接修改左邊的值,不會產生新值。
範例:
  1. use std::ops::{AddAssign, MulAssign};
  2. #[derive(Debug, Clone, Copy)]
  3. struct Money {
  4. cents: i64,
  5. }
  6. impl AddAssign for Money {
  7. fn add_assign(&mut self, other: Money) {
  8. self.cents += other.cents;
  9. }
  10. }
  11. impl MulAssign<i64> for Money {
  12. fn mul_assign(&mut self, factor: i64) {
  13. self.cents *= factor;
  14. }
  15. }
  16. fn main() {
  17. let mut wallet = Money { cents: 1_000 };
  18. wallet += Money { cents: 250 };
  19. println!("{:?}", wallet);
  20. wallet *= 3;
  21. println!("{:?}", wallet);
  22. }

輸出:
  1. Money { cents: 1250 }
  2. Money { cents: 3750 }

大括號空結構與 encode_utf16


struct Marker {} 與 struct Unit; 都是沒有欄位的結構,建立時分別寫 Marker {} 與 Unit;開發中常在「有欄位」與「沒欄位」之間切換時,大括號寫法改起來比較順。encode_utf16 把字串轉成 UTF-16 編碼單元,和 Windows API 或 JavaScript 交換字串時會用到。
範例:
  1. #[derive(Debug, Default)]
  2. struct Marker {}
  3. #[derive(Debug)]
  4. struct Unit;
  5. fn main() {
  6. let a = Marker {};
  7. let b = Marker::default();
  8. let c = Unit;
  9. println!("{:?} {:?} {:?}", a, b, c);
  10. let units: Vec<u16> = "Rust🦀".encode_utf16().collect();
  11. println!("{} 個 UTF-16 單元:{:x?}", units.len(), units);
  12. }

輸出:
  1. Marker Marker Unit
  2. 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::fsMetadataExt::as_raw_statdeprecated in favor of the accessor methods of this trait
std::os::linux::fsMetadataExt::as_raw_statother methods of this trait are now preferred
std::os::linux::rawblkcnt_tthese 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::rawblksize_tthese 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::rawdev_tthese 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::rawino_tthese 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::rawmode_tthese 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::rawnlink_tthese 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::rawoff_tthese 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::rawpthread_tthese 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::rawstatthese 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::rawtime_tthese 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::rawblkcnt_tthese 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::rawblksize_tthese 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::rawdev_tthese 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::rawgid_tthese 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::rawino_tthese 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::rawmode_tthese 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::rawnlink_tthese 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::rawoff_tthese 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::rawpid_tthese 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::rawpthread_tthese 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::rawtime_tthese 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::rawuid_tthese 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-5222cargoCargo can be coerced to share credentials between registries早於 1.96.01.96.0
CVE-2026-5223cargoCrates in third party registries can override the cached source of other crates早於 1.96.01.96.0
CVE-2024-43402stdstd::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods早於 1.81.01.81.0
CVE-2024-24576stdstd::process::Command did not properly escape arguments of batch files on Windows早於 1.77.21.77.2
CVE-2023-38497cargoCargo not respecting umask when extracting crate archives早於 1.71.11.71.1
CVE-2022-46176cargoCargo did not verify SSH host keys早於 1.66.11.66.1
CVE-2022-36113cargoExtracting malicious crates can corrupt arbitrary files早於 1.64.01.64.0
CVE-2022-36114cargoExtracting malicious crates can fill the file system早於 1.64.01.64.0
CVE-2022-21658stdRace condition in std::fs::remove_dir_all早於 1.58.11.58.1
CVE-2021-42574rustcrustc and bidirectional-override codepoints in source code早於 1.56.11.56.1
CVE-2021-29922stdImproper Input Validation of octal literals in std::net早於 1.53.01.53.0
CVE-2019-16760cargoCargo prior to Rust 1.26.0 may download the wrong dependency早於 1.26.01.26.0
CVE-2018-1000657stdBuffer overflow vulnerability in VecDeque::reserve()1.3.0 起,早於 1.22.01.22.0
CVE-2018-1000622rustdocUncontrolled search path element vulnerability in rustdoc plugins早於 1.27.11.27.1
CVE-2018-25008stdInsufficient synchronization in Arc::get_mut1.3.0 起,早於 1.29.01.29.0
CVE-2017-20004stdMutexGuard<Cell<i32>> must not be Sync早於 1.19.01.19.0

本版已停止支援,不會再修正這些漏洞;請升級到 1.100.1。

資料來源