Rust 1.19

最後更新


Rust 1.19 發布於 2017-07-20,目前最新修補版為 1.19.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.19 帶來兩個長期期待的語言功能:與 C 相容的 union,以及 loop 可以用 break 值 回傳結果,讓 loop 也成為有值的運算式。標準庫加入 eprint!/eprintln!,直接輸出到標準錯誤;Windows 上 rustc 也終於能自動找到 Visual Studio 2017 的建置工具。升級時要注意:穩定版與 beta 版不再接受 -Z 不穩定旗標,舊的 cargo-check 外掛因此失效,請改用內建的 cargo check。

語言變更


  • loop 可以用 break 值 回傳值,例如 let x = loop { break 7; };,詳見 迴圈。
  • 新增 union,只能包含 Copy 型別且不能實作 Drop,讀取欄位需要 unsafe,詳見 unsafe。
  • 沒有捕獲環境的閉包可以強制轉型為函式指標 fn,詳見 閉包。
  • 元組結構可以用數字欄位名建立,例如 Point { 0: 7, 1: 0 }。
  • 巨集遞迴上限由 64 提高到 1024,並新增偵測未使用巨集的 lint,詳見 宣告式巨集。

標準庫重點


  • eprint!、eprintln! 加入 prelude,用法與 print! 相同但寫到標準錯誤,詳見 prelude。
  • 新增 cmp::Reverse,把排序方向反過來,詳見 std::cmp。
  • 新增 thread::ThreadId,詳見 std::thread。
  • 新增 Command::envs,一次設定多個環境變數,詳見 std::process。
  • 新增 OsString::shrink_to_fit;String 實作 FromIterator<Cow<str>> 與 Extend<Cow<str>>;Vec 實作 From<&mut [T]>。

Cargo 與工具


  • 建置腳本可以用 println!("cargo:rustc-env=FOO=bar"); 為編譯中的 crate 設定環境變數。
  • 工作區成員可以用 glob 樣式指定;--all 可搭配 --exclude 排除特定套件;cargo bench 支援 --all,詳見 工作區。
  • --features 接受以逗號或空白分隔的多個值;收到 crates.io 的 5xx 錯誤會自動重試。
  • Cargo 不再為 crates.io 索引建立本機工作目錄,註冊表占用空間更小、複製更快。
  • rustc 支援 Visual Studio 2017;改用 LLVM 4.0.1;發布 xz 壓縮的安裝包。

相容性注意


  • 穩定版禁止使用 -Z 旗標。
  • MutexGuard<T> 只有在 T: Sync 時才是 Sync,詳見 Send 與 Sync。
  • 以下由警告改為硬錯誤:在 match 模式中使用浮點數常數、使用沒有 derive PartialEq 與 Eq 的結構或列舉常數、命名為 '_ 的生命週期、在模組外使用私有的 extern crate。
  • use ::self::foo; 與 42._ 這類浮點字面值成為錯誤;重新匯出私有列舉變體也是錯誤。

loop 回傳值與閉包轉函式指標


以前 loop 只是敘述,想從迴圈帶出結果要先宣告變數、在迴圈裡賦值再 break。現在 break 後面可以接一個值,整個 loop 就等於這個值。同一版也允許沒有捕獲任何變數的閉包當作 fn 函式指標傳遞。
範例:
  1. fn apply(f: fn(i32) -> i32, x: i32) -> i32 {
  2. f(x)
  3. }
  4. fn main() {
  5. let data = [3, 8, 15, 4, 22];
  6. let mut i = 0;
  7. let first_big = loop {
  8. if i == data.len() {
  9. break None;
  10. }
  11. if data[i] > 10 {
  12. break Some((i, data[i]));
  13. }
  14. i += 1;
  15. };
  16. println!("第一個大於 10 的:{:?}", first_big);
  17. let double: fn(i32) -> i32 = |x| x * 2;
  18. println!("{}", apply(double, 21));
  19. println!("{}", apply(|x| x - 1, 21));
  20. }

輸出:
  1. 第一個大於 10 的:Some((2, 15))
  2. 42
  3. 20

注意:只有 loop 可以 break 帶值,while 與 for 不行;有捕獲外部變數的閉包不能轉成 fn,要改用泛型或 impl Fn。

union


union 像沒有標籤(tag)的列舉:所有欄位共用同一塊記憶體,程式不會記錄目前存的是哪一個欄位。主要用途是對接 C 函式庫的 union,或做節省空間的底層資料結構。因為編譯器無法確認你讀的欄位是否就是最後寫入的那個,讀取欄位必須放在 unsafe 區塊。
範例:
  1. #[repr(C)]
  2. union IntOrFloat {
  3. i: u32,
  4. f: f32,
  5. }
  6. fn main() {
  7. let mut v = IntOrFloat { f: 1.0 };
  8. let bits = unsafe { v.i };
  9. println!("1.0 的位元:{:#010x}", bits);
  10. v.i = 0x4049_0fdb;
  11. let f = unsafe { v.f };
  12. println!("0x40490fdb 當成 f32:{}", f);
  13. println!("大小:{} bytes", std::mem::size_of::<IntOrFloat>());
  14. }

輸出:
  1. 1.0 的位元:0x3f800000
  2. 0x40490fdb 當成 f32:3.1415927
  3. 大小:4 bytes

注意:讀取一個沒有被寫入有效值的欄位可能是未定義行為;單純想看浮點數的位元,用安全的 f32::to_bits 與 f32::from_bits 即可,詳見 f64 與浮點數。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


影響 1.19.0 的已知漏洞共 20 個;在本版修正的共 0 個;仍影響最新修補版 1.19.0 的共 20 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。

仍影響 1.19.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-28878stdZip may call __iterator_get_unchecked twice with the same index1.14.0 起,早於 1.52.01.52.0
CVE-2021-28879stdZip can cause buffer overflow when a consumed Zip iterator is used again1.14.0 起,早於 1.52.01.52.0
CVE-2021-28876stdPanic safety issue in Zip specialization1.14.0 起,早於 1.52.01.52.0
CVE-2021-28877stdTrustedRandomAccess specialization composes incorrectly for nested iter::Zips1.11.0 起,早於 1.51.01.51.0
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-2019-1010299stdvec_deque::Iter has unsound Debug implementation1.18.0 起,早於 1.30.01.30.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

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

資料來源