Rust 1.63

最後更新


Rust 1.63 發布於 2022-08-11,目前最新修補版為 1.63.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.63 最重要的新功能是作用域執行緒(scoped threads):std::thread::scope 保證裡面開的執行緒在函式返回前全部結束,所以執行緒可以直接借用區域變數,不必再把資料包進 Arc。Mutex::new、RwLock::new、Condvar::new 也能在 const 環境呼叫,全域的 static Mutex 不再需要 lazy_static。這一版還完成了非詞法生命週期(NLL)的遷移,舊的借用檢查器從所有 edition 移除,對既有程式的行為沒有影響。

語言變更


  • 舊的詞法借用檢查器完全移除,所有 edition 都使用 NLL 借用檢查器,觀念見 生命週期。
  • 函式參數有 impl Trait 時,其他泛型參數也可以用 turbofish(foo::<u32>(...))明確指定;impl Trait 本身仍不能指定,泛型見 泛型。
  • 移除標籤與生命週期遮蔽(shadowing)的警告。

標準庫重點


  • thread::scope、thread::Scope、thread::ScopedJoinHandle 穩定,詳見 std::thread 與 執行緒。
  • Mutex::new、RwLock::new、Condvar::new 可在 const 環境使用,詳見 std::sync。
  • array::from_fn 依索引產生陣列的每個元素,詳見 陣列。
  • Path::try_exists 在無法判斷路徑是否存在時回傳錯誤,而不是靜默回傳 false,詳見 std::path。
  • I/O 安全(I/O safety)型別穩定:Unix 的 OwnedFd、BorrowedFd、AsFd,Windows 的 OwnedHandle、BorrowedHandle、OwnedSocket 等,用型別區分「擁有」與「借用」檔案描述子。
  • Box::into_pin、ToOwned::clone_into、Ref::filter_map、RefMut::filter_map、Ipv6Addr::to_ipv4_mapped 一併穩定;str::from_utf8 可在 const 環境使用。
  • VecDeque<u8> 實作 Read 與 Write。

Cargo 與工具


  • --config path 命令列參數穩定,可指定額外的設定檔。
  • Cargo.toml 的 rust-version 會以環境變數 CARGO_PKG_RUST_VERSION 提供給編譯中的程式,Cargo 見 Cargo。

相容性注意


  • 浮點數轉 Duration 改用四捨五入,結果可能與舊版有些微差異,詳見 std::time。
  • #[link] 屬性檢查更嚴格,以前被忽略的無效參數會變成錯誤。

thread::scope:可以借用區域變數的執行緒


thread::spawn 要求閉包是 'static,因為執行緒可能比當前函式活得更久。thread::scope 會在區塊結束前自動 join 所有執行緒,編譯器因此知道借用是安全的:多個執行緒可以同時讀 words 的切片,也能讓單一執行緒可變借用 total。區塊的回傳值會成為 scope 的回傳值。
範例:
  1. use std::thread;
  2. fn main() {
  3. let words = vec!["apple", "banana", "cherry", "date"];
  4. let mut total = 0;
  5. let (left, right) = words.split_at(2);
  6. let (a, b) = thread::scope(|s| {
  7. let h1 = s.spawn(|| left.iter().map(|w| w.len()).sum::<usize>());
  8. let h2 = s.spawn(|| right.iter().map(|w| w.len()).sum::<usize>());
  9. (h1.join().unwrap(), h2.join().unwrap())
  10. });
  11. thread::scope(|s| {
  12. s.spawn(|| total = a + b);
  13. });
  14. println!("{a} {b} {total}");
  15. println!("{words:?}");
  16. }

輸出:
  1. 11 10 21
  2. ["apple", "banana", "cherry", "date"]

注意:同一時間若有兩個執行緒都要可變借用同一個變數,仍會被借用檢查器拒絕;作用域只解決「活得夠久」的問題,不解決資料競爭。

const Mutex::new:免套件的全域鎖


Mutex::new 變成 const fn 後,可以直接寫在 static 的初始值裡,所有執行緒共用同一把鎖保護的資料。輸出前先排序,避免執行緒交錯造成順序不固定。
範例:
  1. use std::sync::Mutex;
  2. use std::thread;
  3. static LOG: Mutex<Vec<String>> = Mutex::new(Vec::new());
  4. fn record(msg: &str) {
  5. LOG.lock().unwrap().push(msg.to_string());
  6. }
  7. fn main() {
  8. thread::scope(|s| {
  9. for i in 0..3 {
  10. s.spawn(move || record(&format!("worker {i}")));
  11. }
  12. });
  13. let mut log = LOG.lock().unwrap();
  14. log.sort();
  15. println!("{log:?}");
  16. }

輸出:
  1. ["worker 0", "worker 1", "worker 2"]

array::from_fn:依索引產生陣列


std::array::from_fn 接受一個以索引 usize 為參數的閉包,呼叫 N 次組成 [T; N]。長度由型別標註決定,元素不必實作 Copy 或 Default,所以也能建立 [String; 3]。
範例:
  1. fn main() {
  2. let squares: [u32; 5] = std::array::from_fn(|i| (i as u32 + 1).pow(2));
  3. println!("{squares:?}");
  4. let labels: [String; 3] = std::array::from_fn(|i| format!("item-{i}"));
  5. println!("{labels:?}");
  6. }

輸出:
  1. [1, 4, 9, 16, 25]
  2. ["item-0", "item-1", "item-2"]

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

仍影響 1.63.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-40030cargoMalicious dependencies can inject arbitrary JavaScript into cargo-generated timing reports1.60.0 起,早於 1.72.01.72.0
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

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

資料來源