Rust 1.18

最後更新


Rust 1.18 發布於 2017-06-08,目前最新修補版為 1.18.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.18 讓 pub 可以加上範圍限制:pub(crate) 表示「整個 crate 內公開、對外不公開」,pub(in 路徑) 只對指定模組樹公開,寫函式庫時更容易劃清內外界線。編譯器也開始自動重排沒有指定 repr 的結構欄位以減少填充(padding),許多型別因此變小;同一版線上文件首次收錄《The Rust Programming Language》第二版草稿。升級時要注意:依賴結構欄位順序的 transmute 一向是未定義行為,現在更可能真的出錯,與 C 互通的結構要加 #[repr(C)]。

語言變更


  • pub(crate)、pub(in path) 等受限可見性(restricted visibility)穩定,詳見 模組系統。
  • 沒有 repr 或為 #[repr(Rust)] 的元組、列舉變體欄位與結構會重排欄位以減少填充,詳見 std::mem。
  • 新增 Windows 專用的 #![windows_subsystem = "windows"] 屬性,圖形介面程式啟動時不會閃出主控台視窗。
  • 巨集的 ty 片段可以接受 Write + Send 這類特徵物件型別,詳見 宣告式巨集。
  • 0e+10 成為合法的浮點數字面值;把生命週期參數綁定到 'static 會產生警告。

標準庫重點


  • 新增 HashMap::retain、HashSet::retain,詳見 HashMap。
  • 新增 Child::try_wait,不阻塞地檢查子行程是否結束,詳見 std::process。
  • 新增 PeekMut::pop,在 peek 過 BinaryHeap 頂端後直接彈出,詳見 std::collections。
  • 新增 TcpStream::peek、UdpSocket::peek、UdpSocket::peek_from,讀取資料但不從緩衝區移除,詳見 std::net。
  • 把未消耗過的 vec::IntoIter 收集回 Vec 時,會直接沿用原本的配置;修正 fs::create_dir_all 的競態條件。

Cargo 與工具


  • 新增 --bins、--examples、--tests、--benches,一次建置某一類的所有目標,詳見 Cargo。
  • cargo new --vcs pijul 支援 Pijul 版本控制;Cargo 支援 Android 與 Haiku。
  • rustc 可以用 --emit mir 輸出 MIR;編譯 rustc 本身快了 15%–20%。
  • rustdoc 新增 --enable-commonmark,改用符合 CommonMark 的 Markdown 解析器。

相容性注意


  • format! 的 0 旗標改為一律把補零放在正負號之後、數字之前;搭配 # 時放在前綴之後,詳見 std::fmt。
  • 欄位重排後,對沒有 repr 的結構使用 transmute 更容易壞掉。
  • 重疊的固有 impl(inherent impl)成為硬錯誤;PartialOrd 與 Ord 的排序結果必須一致。
  • rustc main.rs -o out --emit=asm,llvm-ir 會同時輸出 out.asm 與 out.ll。

pub(crate) 與 pub(in path)


以前一個項目只有「私有」與 pub 兩種選擇,想讓模組間共用、又不想讓函式庫使用者看到,只能靠繞路的模組安排。pub(crate) 讓項目在整個 crate 內可見;pub(in crate::shop) 則只在 shop 模組底下可見,main 呼叫不到。
範例:
  1. mod shop {
  2. pub mod cart {
  3. pub(crate) fn total(prices: &[u32]) -> u32 {
  4. prices.iter().sum::<u32>() + super::tax::fee()
  5. }
  6. }
  7. mod tax {
  8. pub(in crate::shop) fn fee() -> u32 {
  9. 30
  10. }
  11. }
  12. pub fn checkout(prices: &[u32]) -> String {
  13. format!("應付 {} 元(含手續費 {})", cart::total(prices), tax::fee())
  14. }
  15. }
  16. fn main() {
  17. println!("{}", shop::cart::total(&[100, 250]));
  18. println!("{}", shop::checkout(&[100, 250]));
  19. }

輸出:
  1. 380
  2. 應付 380 元(含手續費 30)

注意:pub(in path) 的路徑必須是目前模組的祖先模組;在 main 裡直接呼叫 shop::tax::fee() 會編譯失敗。

自動重排結構欄位


CPU 讀取資料時要求對齊(alignment),u16 要放在 2 的倍數位址,編譯器會在欄位之間塞填充位元組。(u8, u16, u8) 照順序擺要 6 bytes,重排成 (u8, u8, u16) 只要 4 bytes。Rust 從未承諾預設的記憶體佈局,因此這版開始由編譯器自動重排;需要固定順序(例如傳給 C)時加 #[repr(C)],詳見 FFI。
以下數值為 64 位元平台的結果。
範例:
  1. use std::mem::size_of;
  2. #[allow(dead_code)]
  3. struct Suboptimal(u8, u16, u8);
  4. #[allow(dead_code)]
  5. #[repr(C)]
  6. struct CLayout(u8, u16, u8);
  7. fn main() {
  8. println!("Suboptimal:{} bytes", size_of::<Suboptimal>());
  9. println!("CLayout:{} bytes", size_of::<CLayout>());
  10. println!("(u8, u32, u8):{} bytes", size_of::<(u8, u32, u8)>());
  11. }

輸出:
  1. Suboptimal:4 bytes
  2. CLayout:6 bytes
  3. (u8, u32, u8):8 bytes

注意:預設佈局沒有任何保證,可能隨編譯器版本改變;不要在 unsafe 程式碼裡假設欄位的位移,詳見 unsafe。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.18.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
CVE-2017-20004stdMutexGuard<Cell<i32>> must not be Sync早於 1.19.01.19.0

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

資料來源