Rust 1.4

最後更新


Rust 1.4 發布於 2015-10-29,目前最新修補版為 1.4.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.4 以改進與穩定化為主:Windows 上使用 64 位元 MSVC ABI 與連結器的版本正式支援並成為建議選項,Rc/Arc 的 make_mut、try_unwrap、downgrade 等 48 個 API 穩定。RFC 1214 修補型別系統的健全性問題,這版先對違規程式碼發出警告、下一版起改為錯誤。升級要注意:str::lines 與 BufRead::lines 會把 \r\n 也當成換行,只在 Linux/macOS 測過的程式在 Windows 上的行為會改變。

語言變更


  • use 一次匯入多個項目時可以各自改名,例如 use foo::{bar as kitten, baz as puppy},詳見 模組系統。
  • 胖指標(fat pointer)的二元運算正確運作。
  • pub extern crate 的行為不如預期,會發出警告。

標準庫重點


  • Rc 與 Arc 新增 downgrade、get_mut、make_mut、try_unwrap,rc::Weak 與 sync::Weak 可用 upgrade 取回強參考,詳見 Rc。
  • Box::from_raw、Box::into_raw 與 CString::from_raw、CString::into_raw 穩定,方便和 C 交換所有權,詳見 Box 與 FFI。
  • Result::expect 穩定,失敗時帶著自訂訊息 panic,詳見 Result。
  • Vec::append、Vec::split_off、VecDeque::retain 等集合方法穩定,詳見 Vec。
  • str::split_at 在指定位置把字串切成兩半,Chars::as_str 取回剩下的字串,詳見 &str。
  • thread::sleep 與 thread::park_timeout 穩定,以 Duration 指定時間長度,詳見 std::thread。
  • TcpStream、UdpSocket 可設定讀寫逾時,詳見 std::net。
  • Default 實作到 [T; 32] 為止的陣列;Option 與 Result 的參考實作 IntoIterator。

Cargo 與工具


  • cargo update 會顯示每個相依從哪個版本更新到哪個版本,詳見 Cargo。
  • 編譯器不再使用 morestack 防止堆疊溢位,改用 guard page 與 stack probe(stack probe 目前只在 Windows 實作)。

相容性注意


  • RFC 1214 的型別健全性修正大多先以警告呈現,下一版會變成錯誤,仍有少數情況會立即無法編譯。
  • str::lines 與 BufRead::lines 把 \r\n 當成換行。
  • 'static 生命週期的借用會延續到函式結尾。
  • str::parse 解析浮點數不再引入可避免的捨入誤差,f.to_string().parse() 能精確還原原值;開頭的 + 號也被接受,詳見 型別轉換。
  • HashMap 與 HashSet 實作 Extend<&T>(T: Copy),少數情況下型別推論會失敗。

use 匯入時改名


std::fmt::Write 與 std::io::Write 同名,Result 也有好幾個。1.4 起可以在同一個 use 大括號裡為每個項目取別名,兩組同名項目就能在同一個檔案共存。
範例:
  1. use std::fmt::{Result as FmtResult, Write as FmtWrite};
  2. use std::io::{Result as IoResult, Write as IoWrite};
  3. fn build(out: &mut String) -> FmtResult {
  4. write!(out, "fmt 寫入字串")
  5. }
  6. fn emit(out: &mut Vec<u8>) -> IoResult<()> {
  7. write!(out, "io 寫入位元組")
  8. }
  9. fn main() {
  10. let mut s = String::new();
  11. build(&mut s).unwrap();
  12. println!("{}", s);
  13. let mut buf = Vec::new();
  14. emit(&mut buf).unwrap();
  15. println!("{}", String::from_utf8(buf).unwrap());
  16. }

輸出:
  1. fmt 寫入字串
  2. io 寫入位元組

Rc::make_mut 與 Weak


Rc::make_mut 是「寫入時才複製」(copy-on-write):只有自己持有時直接修改,和別人共享時先複製一份再改,不影響其他持有者。Rc::downgrade 產生不增加強參考計數的 Weak,值被釋放後 upgrade 會回傳 None;Rc::try_unwrap 在只剩一個強參考時取回內部的值。
範例:
  1. use std::rc::Rc;
  2. fn main() {
  3. let original = Rc::new(vec![1, 2, 3]);
  4. let mut copy = Rc::clone(&original);
  5. Rc::make_mut(&mut copy).push(4);
  6. println!("original = {:?}", original);
  7. println!("copy = {:?}", copy);
  8. let weak = Rc::downgrade(&copy);
  9. println!("升級成功:{:?}", weak.upgrade());
  10. drop(copy);
  11. println!("釋放後:{:?}", weak.upgrade());
  12. println!("{:?}", Rc::try_unwrap(original));
  13. }

輸出:
  1. original = [1, 2, 3]
  2. copy = [1, 2, 3, 4]
  3. 升級成功:Some([1, 2, 3, 4])
  4. 釋放後:None
  5. Ok([1, 2, 3])

lines 認得 \r\n


Windows 文字檔的換行是 \r\n。1.4 起 lines 會一併去掉 \r,同一段程式在各平台得到相同的結果;同一版的 split_at 和浮點數解析修正也一起示範如下。
範例:
  1. fn main() {
  2. let windows_text = "first\r\nsecond\r\nthird";
  3. for line in windows_text.lines() {
  4. println!("[{}] 長度 {}", line, line.len());
  5. }
  6. let (left, right) = "hello,world".split_at(5);
  7. println!("{:?} {:?}", left, right);
  8. let x: f64 = "+0.1".parse().unwrap();
  9. println!("{}", x.to_string().parse::<f64>().unwrap() == x);
  10. }

輸出:
  1. [first] 長度 5
  2. [second] 長度 6
  3. [third] 長度 5
  4. "hello" ",world"
  5. true

注意:split_at 的位置必須落在 UTF-8 字元邊界上,否則會 panic。

本版新增的棄用 API


以下 API 從 Rust 1.4 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdstr::lines_anyuse lines() instead now
std::strLinesAnyuse lines()/Lines instead now
std::stringString::lines_anyuse lines() instead now

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.4.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。

資料來源