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 大括號裡為每個項目取別名,兩組同名項目就能在同一個檔案共存。範例:
- use std::fmt::{Result as FmtResult, Write as FmtWrite};
- use std::io::{Result as IoResult, Write as IoWrite};
- fn build(out: &mut String) -> FmtResult {
- write!(out, "fmt 寫入字串")
- }
- fn emit(out: &mut Vec<u8>) -> IoResult<()> {
- write!(out, "io 寫入位元組")
- }
- fn main() {
- let mut s = String::new();
- build(&mut s).unwrap();
- println!("{}", s);
- let mut buf = Vec::new();
- emit(&mut buf).unwrap();
- println!("{}", String::from_utf8(buf).unwrap());
- }
輸出:
- fmt 寫入字串
- io 寫入位元組
Rc::make_mut 與 Weak
Rc::make_mut 是「寫入時才複製」(copy-on-write):只有自己持有時直接修改,和別人共享時先複製一份再改,不影響其他持有者。Rc::downgrade 產生不增加強參考計數的 Weak,值被釋放後 upgrade 會回傳 None;Rc::try_unwrap 在只剩一個強參考時取回內部的值。範例:
- use std::rc::Rc;
- fn main() {
- let original = Rc::new(vec![1, 2, 3]);
- let mut copy = Rc::clone(&original);
- Rc::make_mut(&mut copy).push(4);
- println!("original = {:?}", original);
- println!("copy = {:?}", copy);
- let weak = Rc::downgrade(©);
- println!("升級成功:{:?}", weak.upgrade());
- drop(copy);
- println!("釋放後:{:?}", weak.upgrade());
- println!("{:?}", Rc::try_unwrap(original));
- }
輸出:
- original = [1, 2, 3]
- copy = [1, 2, 3, 4]
- 升級成功:Some([1, 2, 3, 4])
- 釋放後:None
- Ok([1, 2, 3])
lines 認得 \r\n
Windows 文字檔的換行是
\r\n。1.4 起 lines 會一併去掉 \r,同一段程式在各平台得到相同的結果;同一版的 split_at 和浮點數解析修正也一起示範如下。範例:
- fn main() {
- let windows_text = "first\r\nsecond\r\nthird";
- for line in windows_text.lines() {
- println!("[{}] 長度 {}", line, line.len());
- }
- let (left, right) = "hello,world".split_at(5);
- println!("{:?} {:?}", left, right);
- let x: f64 = "+0.1".parse().unwrap();
- println!("{}", x.to_string().parse::<f64>().unwrap() == x);
- }
輸出:
- [first] 長度 5
- [second] 長度 6
- [third] 長度 5
- "hello" ",world"
- true
注意:
split_at 的位置必須落在 UTF-8 字元邊界上,否則會 panic。本版新增的棄用 API
以下 API 從 Rust 1.4 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std | str::lines_any | use lines() instead now |
std::str | LinesAny | use lines()/Lines instead now |
std::string | String::lines_any | use lines() instead now |
修補版本
本版沒有修補版本。
已知漏洞
影響
1.4.0 的已知漏洞共 16 個;在本版修正的共 0 個;仍影響最新修補版 1.4.0 的共 16 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。仍影響 1.4.0
| 編號 | 元件 | 摘要(官方原文) | 影響範圍 | 修正於 |
|---|---|---|---|---|
| CVE-2026-5222 | cargo | Cargo can be coerced to share credentials between registries | 早於 1.96.0 | 1.96.0 |
| CVE-2026-5223 | cargo | Crates in third party registries can override the cached source of other crates | 早於 1.96.0 | 1.96.0 |
| CVE-2024-43402 | std | std::process::Command batch files argument escaping could be bypassed with trailing whitespace or periods | 早於 1.81.0 | 1.81.0 |
| CVE-2024-24576 | std | std::process::Command did not properly escape arguments of batch files on Windows | 早於 1.77.2 | 1.77.2 |
| CVE-2023-38497 | cargo | Cargo not respecting umask when extracting crate archives | 早於 1.71.1 | 1.71.1 |
| CVE-2022-46176 | cargo | Cargo did not verify SSH host keys | 早於 1.66.1 | 1.66.1 |
| CVE-2022-36113 | cargo | Extracting malicious crates can corrupt arbitrary files | 早於 1.64.0 | 1.64.0 |
| CVE-2022-36114 | cargo | Extracting malicious crates can fill the file system | 早於 1.64.0 | 1.64.0 |
| CVE-2022-21658 | std | Race condition in std::fs::remove_dir_all | 早於 1.58.1 | 1.58.1 |
| CVE-2021-42574 | rustc | rustc and bidirectional-override codepoints in source code | 早於 1.56.1 | 1.56.1 |
| CVE-2021-29922 | std | Improper Input Validation of octal literals in std::net | 早於 1.53.0 | 1.53.0 |
| CVE-2019-16760 | cargo | Cargo prior to Rust 1.26.0 may download the wrong dependency | 早於 1.26.0 | 1.26.0 |
| CVE-2018-1000657 | std | Buffer overflow vulnerability in VecDeque::reserve() | 1.3.0 起,早於 1.22.0 | 1.22.0 |
| CVE-2018-1000622 | rustdoc | Uncontrolled search path element vulnerability in rustdoc plugins | 早於 1.27.1 | 1.27.1 |
| CVE-2018-25008 | std | Insufficient synchronization in Arc::get_mut | 1.3.0 起,早於 1.29.0 | 1.29.0 |
| CVE-2017-20004 | std | MutexGuard<Cell<i32>> must not be Sync | 早於 1.19.0 | 1.19.0 |
本版已停止支援,不會再修正這些漏洞;請升級到
1.100.1。