Rust 1.99

最後更新


Rust 1.99 發布於 2026-10-01,目前最新修補版為 1.99.1,已停止支援,請升級到 1.100.1。

版本介紹


Rust 1.99 穩定以 "C" 與 "C-unwind" ABI 在 Rust 中「定義」C 風格的可變參數函式:以前 Rust 只能呼叫外部的 printf 這類函式,現在可以自己寫一個,參數 ... 的型別是與 C va_list 相容的 VaList。標準庫也釐清了從原始指標取得大小與對齊的安全條件,並新增 String::from_utf8_lossy_owned 等實用 API。升級時要注意:std::i32::MAX 這類舊的整數模組常數全面棄用,請改寫成 i32::MAX;#[no_mangle] 用在泛型項目上變成硬錯誤;在 CI 環境(有 CI 環境變數)下 Cargo 預設關閉增量編譯。

語言變更


  • 穩定 C 風格可變參數函式的定義("C"、"C-unwind" ABI),以及用 #[unsafe(naked)] 裸函式定義其他 ABI 的可變參數函式,詳見 FFI。
  • 穩定 #[my_macro] mod foo;:外部檔案模組可以出現在屬性巨集或 derive 巨集的輸入中,詳見程序式巨集。
  • 保證 UnsafeCell 的內容可以不經 get 存取;新增預設關閉的 raw_borrows_via_references lint,詳見 unsafe。
  • unconditional_panic lint 擴及 chunks/windows 大小為零的呼叫;unreachable_cfg_select_predicates 加入 unused lint 群組。
  • 缺少方法時,編譯器的建議會優先採用完全相符的 doc alias。

標準庫重點


  • core::ffi::VaList,詳見 std::ffi。
  • String::from_utf8_lossy_owned 與 FromUtf8Error::into_utf8_lossy,詳見 String。
  • mem::size_of_val_raw、mem::align_of_val_raw、Layout::for_value_raw,詳見 std::mem。
  • Vec::into_parts、Vec::from_parts、Box::into_non_null、Box::from_non_null;Box<[T; N]> 實作 IntoIterator,詳見 Box。
  • std::fs::set_times、set_times_nofollow 設定檔案時間,詳見 std::fs。
  • LocalKey<Cell<T>>::update、VecDeque::retain_back、StepBy 實作 FusedIterator。
  • core::fmt::NumBuffer 也從 std::fmt 重新匯出;RangeInclusive 迭代最佳化,已耗盡範圍的 start()/end() 回傳值可能改變。
  • Box::leak 的文件建議不要再把洩漏的記憶體轉回 Box 釋放,改用 Box::into_non_null 或 Box::into_raw。

Cargo 與工具


  • 新增內建 profile debug,目前與 dev 完全相同,為日後調整 dev 做準備,詳見 Cargo。
  • 2024 edition 以上的工作區成員可以覆寫繼承相依的 default-features,詳見工作區。
  • 偵測到 CI 環境變數時,Cargo 預設關閉增量編譯。
  • rustdoc 新增 unused_footnote_definition lint,trait impl 過濾平均快 20%。

相容性注意


  • 舊的整數模組(如 std::i32::MAX)全面棄用,改用 i32::MAX,詳見棄用 API。
  • no_mangle_generic_items 升級為硬錯誤。
  • 來自其他 crate、展開結果以分號結尾的巨集也會觸發警告(semicolon_in_expressions_from_non_local_macros),請回報給巨集的提供者。
  • 文件測試中沒有套用對象的屬性會報錯,詳見文件測試。
  • Pin::new_unchecked 的安全不變條件略有調整,詳見 std::pin。

用 Rust 定義 C 可變參數函式


函式參數最後寫 mut args: ...,就能接收任意數量的參數;args 的型別是 VaList,用 next_arg::<型別>() 依序讀出。函式必須是 unsafe extern "C",因為呼叫端要保證實際傳入的參數個數與型別正確,編譯器無法檢查。範例以第一個參數告訴函式後面有幾個 i32。
範例:
  1. unsafe extern "C" fn sum(count: i32, mut args: ...) -> i32 {
  2. let mut total = 0;
  3. for _ in 0..count {
  4. total += unsafe { args.next_arg::<i32>() };
  5. }
  6. total
  7. }
  8. fn main() {
  9. let a = unsafe { sum(3, 10i32, 20i32, 30i32) };
  10. let b = unsafe { sum(0) };
  11. println!("{a} {b}");
  12. }

輸出:
  1. 60 0

注意:讀取超過實際傳入個數的參數,或用錯型別讀取,都是未定義行為,詳見 unsafe。

String::from_utf8_lossy_owned


String::from_utf8_lossy 回傳 Cow<str>,若想要 String 還得再呼叫 into_owned()。from_utf8_lossy_owned 直接接收 Vec<u8> 並回傳 String,不合法的位元組以 �(U+FFFD)取代。from_utf8 失敗時得到的 FromUtf8Error 也能用 into_utf8_lossy 轉成同樣修補過的 String。
範例:
  1. fn main() {
  2. let bytes = vec![b'h', b'i', 0xFF, b'!'];
  3. let text: String = String::from_utf8_lossy_owned(bytes);
  4. println!("{text}");
  5. let bad = vec![0xE5, 0x8F, b'A'];
  6. match String::from_utf8(bad) {
  7. Ok(s) => println!("合法:{s}"),
  8. Err(e) => println!("修補後:{}", e.into_utf8_lossy()),
  9. }
  10. }

輸出:
  1. hi�!
  2. 修補後:�A

本版新增的棄用 API


以下 API 從 Rust 1.99 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
模組項目官方說明
stdi128::max_valuereplaced by the MAX associated constant on this type
stdi128::min_valuereplaced by the MIN associated constant on this type
stdi16::max_valuereplaced by the MAX associated constant on this type
stdi16::min_valuereplaced by the MIN associated constant on this type
stdi32::max_valuereplaced by the MAX associated constant on this type
stdi32::min_valuereplaced by the MIN associated constant on this type
stdi64::max_valuereplaced by the MAX associated constant on this type
stdi64::min_valuereplaced by the MIN associated constant on this type
stdi8::max_valuereplaced by the MAX associated constant on this type
stdi8::min_valuereplaced by the MIN associated constant on this type
stdisize::max_valuereplaced by the MAX associated constant on this type
stdisize::min_valuereplaced by the MIN associated constant on this type
stdu128::max_valuereplaced by the MAX associated constant on this type
stdu128::min_valuereplaced by the MIN associated constant on this type
stdu16::max_valuereplaced by the MAX associated constant on this type
stdu16::min_valuereplaced by the MIN associated constant on this type
stdu32::max_valuereplaced by the MAX associated constant on this type
stdu32::min_valuereplaced by the MIN associated constant on this type
stdu64::max_valuereplaced by the MAX associated constant on this type
stdu64::min_valuereplaced by the MIN associated constant on this type
stdu8::max_valuereplaced by the MAX associated constant on this type
stdu8::min_valuereplaced by the MIN associated constant on this type
stdusize::max_valuereplaced by the MAX associated constant on this type
stdusize::min_valuereplaced by the MIN associated constant on this type
std::f32DIGITSreplaced by the DIGITS associated constant on f32
std::f32EPSILONreplaced by the EPSILON associated constant on f32
std::f32INFINITYreplaced by the INFINITY associated constant on f32
std::f32MANTISSA_DIGITSreplaced by the MANTISSA_DIGITS associated constant on f32
std::f32MAXreplaced by the MAX associated constant on f32
std::f32MAX_10_EXPreplaced by the MAX_10_EXP associated constant on f32
std::f32MAX_EXPreplaced by the MAX_EXP associated constant on f32
std::f32MINreplaced by the MIN associated constant on f32
std::f32MIN_10_EXPreplaced by the MIN_10_EXP associated constant on f32
std::f32MIN_EXPreplaced by the MIN_EXP associated constant on f32
std::f32MIN_POSITIVEreplaced by the MIN_POSITIVE associated constant on f32
std::f32NANreplaced by the NAN associated constant on f32
std::f32NEG_INFINITYreplaced by the NEG_INFINITY associated constant on f32
std::f32RADIXreplaced by the RADIX associated constant on f32
std::f64DIGITSreplaced by the DIGITS associated constant on f64
std::f64EPSILONreplaced by the EPSILON associated constant on f64
std::f64INFINITYreplaced by the INFINITY associated constant on f64
std::f64MANTISSA_DIGITSreplaced by the MANTISSA_DIGITS associated constant on f64
std::f64MAXreplaced by the MAX associated constant on f64
std::f64MAX_10_EXPreplaced by the MAX_10_EXP associated constant on f64
std::f64MAX_EXPreplaced by the MAX_EXP associated constant on f64
std::f64MINreplaced by the MIN associated constant on f64
std::f64MIN_10_EXPreplaced by the MIN_10_EXP associated constant on f64
std::f64MIN_EXPreplaced by the MIN_EXP associated constant on f64
std::f64MIN_POSITIVEreplaced by the MIN_POSITIVE associated constant on f64
std::f64NANreplaced by the NAN associated constant on f64
std::f64NEG_INFINITYreplaced by the NEG_INFINITY associated constant on f64
std::f64RADIXreplaced by the RADIX associated constant on f64
std::i128MAXreplaced by the MAX associated constant on this type
std::i128MINreplaced by the MIN associated constant on this type
std::i16MAXreplaced by the MAX associated constant on this type
std::i16MINreplaced by the MIN associated constant on this type
std::i32MAXreplaced by the MAX associated constant on this type
std::i32MINreplaced by the MIN associated constant on this type
std::i64MAXreplaced by the MAX associated constant on this type
std::i64MINreplaced by the MIN associated constant on this type
std::i8MAXreplaced by the MAX associated constant on this type
std::i8MINreplaced by the MIN associated constant on this type
std::isizeMAXreplaced by the MAX associated constant on this type
std::isizeMINreplaced by the MIN associated constant on this type
std::sync::atomicAtomic::fetch_updaterenamed to try_update for consistency
std::u128MAXreplaced by the MAX associated constant on this type
std::u128MINreplaced by the MIN associated constant on this type
std::u16MAXreplaced by the MAX associated constant on this type
std::u16MINreplaced by the MIN associated constant on this type
std::u32MAXreplaced by the MAX associated constant on this type
std::u32MINreplaced by the MIN associated constant on this type
std::u64MAXreplaced by the MAX associated constant on this type
std::u64MINreplaced by the MIN associated constant on this type
std::u8MAXreplaced by the MAX associated constant on this type
std::u8MINreplaced by the MIN associated constant on this type
std::usizeMAXreplaced by the MAX associated constant on this type
std::usizeMINreplaced by the MIN associated constant on this type

修補版本


版本發布日期含安全修補內容(官方原文)
1.99.12026-10-15否library: fix missing symbol wcslen on UEFI and embedded targets;rustc: fix miscompilation of FFI bool return values on x86_64;rust-lld: add the rpath for libLLVM on macOS

已知漏洞


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

資料來源