Rust 1.78

最後更新


Rust 1.78 發布於 2024-05-02,目前最新修補版為 1.78.0,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.78 穩定了 #[diagnostic] 屬性命名空間,函式庫作者可以用 #[diagnostic::on_unimplemented] 自訂「型別沒有實作某特徵」時的錯誤訊息。對新手來說,這代表用到設計良好的函式庫時會看到更易懂的編譯錯誤;同時開啟除錯斷言(debug build)時,標準庫會在更多 unsafe 函式的前置條件上檢查,及早抓出未定義行為。升級時要注意:Windows tier 1 目標至少需要 Windows 10,wasm32-wasi 開始改名為 wasm32-wasip1。

語言變更


  • 穩定 #[diagnostic] 命名空間與 #[diagnostic::on_unimplemented] 屬性,詳見 特徵進階。
  • 對 NaN 做模式比對改為硬錯誤,詳見 浮點數。
  • 穩定 #[cfg(target_abi = ...)]。
  • 新增 non_contiguous_range_endpoints lint,提醒排除區間模式後漏掉單一值的情況。
  • trait Trait: Auto {} 可以把 dyn Trait 向上轉成 dyn Trait + Auto。

標準庫重點


  • 開啟除錯斷言時,許多 unsafe 函式的前置條件會在執行期檢查,詳見 unsafe。
  • align_offset 與 align_to 在所有情況下都有確定的行為,詳見 std::ptr。
  • &Stdin 實作 Read;io::Error 實作 From<TryReserveError>,詳見 std::io。
  • Unicode 更新到 15.1.0;pthread 版本的 RwLock 改用自家實作。

Cargo 與工具


  • 穩定 lockfile v4,產生 lockfile 時會參考 rust-version,詳見 Cargo 與套件。
  • 官方支援 target.<triple>.rustdocflags 設定。
  • 編譯器更新到 LLVM 18。

相容性注意


  • Windows tier 1 目標至少需要 Windows 10。
  • 新增 wasm32-wasip1 目標,wasm32-wasi-preview1-threads 改名為 wasm32-wasip1-threads。
  • INVALID_DOC_ATTRIBUTES lint 改為預設拒絕。
  • 修正 const 初始化中內部可變性的偵測,部分原本被誤接受的泛型程式碼不再能編譯。

自訂未實作特徵的錯誤訊息


在特徵上加 #[diagnostic::on_unimplemented],就能在使用者傳錯型別時顯示自己的 message、label 與 note,{Self} 會被換成實際的型別。下面這段無法編譯:42 沒有實作 Storable,錯誤訊息改用特徵作者寫的文字。
範例:
  1. #[diagnostic::on_unimplemented(
  2. message = "`{Self}` 不能存進背包",
  3. label = "這個型別沒有實作 Storable",
  4. note = "替 `{Self}` 實作 `Storable` 特徵後再試一次"
  5. )]
  6. trait Storable {}
  7. struct Sword;
  8. impl Storable for Sword {}
  9. fn store<T: Storable>(_item: T) {}
  10. fn main() {
  11. store(Sword);
  12. store(42);
  13. }

輸出:
  1. error[E0277]: `{integer}` 不能存進背包
  2. --> main.rs:15:11
  3. |
  4. 15 | store(42);
  5. | ----- ^^ 這個型別沒有實作 Storable
  6. | |
  7. | required by a bound introduced by this call
  8. |
  9. = help: the trait `Storable` is not implemented for `{integer}`
  10. = note: 替 `{integer}` 實作 `Storable` 特徵後再試一次

注意:#[diagnostic] 只是給編譯器的提示,編譯器不一定採用;寫了編譯器不認得的診斷屬性也不算錯誤。錯誤訊息的排版細節之後仍可能調整,行號會依你的檔案而異。

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


本版沒有修補版本。

已知漏洞


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

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

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

資料來源