Rust 1.3

最後更新


Rust 1.3 發布於 2015-09-17,目前最新修補版為 1.3.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.3 穩定了表示時間長度的 Duration 與 std::time 模組,Error 特徵物件也能像 Any 一樣向下轉型(downcast)回具體型別。官方文件新增專講 unsafe Rust 的《The Rustonomicon》,子字串搜尋改用 two-way 演算法而大幅加速。升級要注意:新的物件生命週期預設規則正式生效,&'a Box<Trait> 會被解讀成 &'a Box<Trait + 'static>,少數原本能編譯的程式會因 dropck 規則更新而失敗。

語言變更


  • 新的物件生命週期預設(object lifetime default)規則在一個週期的警告後正式啟用,詳見 特徵物件。
  • 巨集中的型別與路徑後面可以接分號,詳見 宣告式巨集。
  • 'static 變數可以遞迴參考自己。
  • ref 綁定會正確選擇 Deref 或 DerefMut,詳見 模式。
  • 檢查解構子不能存取已銷毀值的 dropck 規則已重寫並對齊 RFC。

標準庫重點


  • Duration 與 std::time 模組穩定(目前模組內只有 Duration),詳見 std::time。
  • Error 特徵物件新增 is、downcast、downcast_ref、downcast_mut,詳見 std::error。
  • io::Error 新增 get_ref、get_mut、into_inner 取得內部錯誤,詳見 std::io。
  • process::Child 新增 id,回傳平台的行程識別碼,詳見 std::process。
  • contains、find、rfind、split 等子字串搜尋改用 two-way 演算法,starts_with/ends_with 也變快,詳見 &str。
  • Hash 特徵新增預設方法 hash_slice,Hasher 新增針對原始型別的 write_* 方法,詳見 std::hash。
  • Box<str> 與 Box<[T]> 實作 Clone;String 實作 DerefMut。
  • CString 實作 Borrow、CStr 實作 ToOwned,C 字串能在泛型程式碼中借用與複製,詳見 std::ffi。
  • Wrapping 型別支援 Div 運算子;AtomicPtr 實作 Debug 與 Send。

Cargo 與工具


  • 新書《The Rustonomicon》深入介紹 unsafe Rust,詳見 unsafe。
  • Cargo 支援 lint 上限(lint capping):相依套件的 lint 不再影響自己專案能否乾淨編譯。
  • 編譯器新增許多延伸錯誤說明,可用 --explain 查看。
  • 經過一些調整後可產生在 Windows XP 上執行的程式,但 XP 不是官方支援的平台。

相容性注意


  • 物件生命週期預設規則改變,已知有一個尚未決定解法的回歸(regression)。
  • size_of_val 與 align_of_val 對動態大小型別的行為改為更合理的結果,詳見 std::mem。
  • dropck 規則更新修補了健全性(soundness)漏洞,部分原本能編譯的程式碼會失敗。
  • 內部屬性 #[prelude_import] 先前被意外穩定,現改放在功能閘之後。

Duration 表示時間長度


Duration 是「一段時間」而不是「某個時間點」,由秒與奈秒組成,可以相加、乘上整數、互相比較。標準庫的逾時、睡眠等 API 都以它為參數。
範例:
  1. use std::time::Duration;
  2. fn main() {
  3. let timeout = Duration::new(2, 500_000_000);
  4. println!("{:?}", timeout);
  5. println!("{} 秒又 {} 奈秒", timeout.as_secs(), timeout.subsec_nanos());
  6. let step = Duration::from_millis(250);
  7. let total = step * 4 + timeout;
  8. println!("{:?}", total);
  9. println!("{}", total > Duration::from_secs(3));
  10. }

輸出:
  1. 2.5s
  2. 2 秒又 500000000 奈秒
  3. 3.5s
  4. true

注意:Duration 不能是負數;相減結果小於零時會 panic,需要時改用 checked_sub。

Error 向下轉型


函式回傳 Box<dyn Error> 可以裝任何錯誤,但呼叫端有時要知道「到底是哪一種錯誤」。downcast_ref::<T>() 在型別相符時回傳 Some(&T),is::<T>() 只回答是或不是,讓呼叫端依錯誤種類分別處理。
範例:
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::num::ParseIntError;
  4. #[derive(Debug)]
  5. struct NotPositive(i64);
  6. impl fmt::Display for NotPositive {
  7. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  8. write!(f, "{} 不是正數", self.0)
  9. }
  10. }
  11. impl Error for NotPositive {}
  12. fn parse_positive(s: &str) -> Result<i64, Box<dyn Error>> {
  13. let n: i64 = s.parse()?;
  14. if n <= 0 {
  15. return Err(Box::new(NotPositive(n)));
  16. }
  17. Ok(n)
  18. }
  19. fn main() {
  20. for input in ["42", "-7", "abc"] {
  21. match parse_positive(input) {
  22. Ok(n) => println!("{input}: 成功 {n}"),
  23. Err(e) => {
  24. if let Some(np) = e.downcast_ref::<NotPositive>() {
  25. println!("{input}: 業務錯誤,值為 {}", np.0);
  26. } else if e.is::<ParseIntError>() {
  27. println!("{input}: 格式錯誤:{e}");
  28. }
  29. }
  30. }
  31. }
  32. }

輸出:
  1. 42: 成功 42
  2. -7: 業務錯誤,值為 -7
  3. abc: 格式錯誤:invalid digit found in string

本版新增的棄用 API


以下 API 從 Rust 1.3 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdslice::connectrenamed to join
std::bstrByteStr::connectrenamed to join
std::bstrByteString::connectrenamed to join
std::ioIoSlice::connectrenamed to join
std::ioIoSliceMut::connectrenamed to join
std::vecVec::connectrenamed to join

修補版本


本版沒有修補版本。

已知漏洞


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

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

資料來源