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_referenceslint,詳見 unsafe。 unconditional_paniclint 擴及 chunks/windows 大小為零的呼叫;unreachable_cfg_select_predicates加入unusedlint 群組。- 缺少方法時,編譯器的建議會優先採用完全相符的 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_definitionlint,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。範例:
- unsafe extern "C" fn sum(count: i32, mut args: ...) -> i32 {
- let mut total = 0;
- for _ in 0..count {
- total += unsafe { args.next_arg::<i32>() };
- }
- total
- }
- fn main() {
- let a = unsafe { sum(3, 10i32, 20i32, 30i32) };
- let b = unsafe { sum(0) };
- println!("{a} {b}");
- }
輸出:
- 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。範例:
- fn main() {
- let bytes = vec![b'h', b'i', 0xFF, b'!'];
- let text: String = String::from_utf8_lossy_owned(bytes);
- println!("{text}");
- let bad = vec![0xE5, 0x8F, b'A'];
- match String::from_utf8(bad) {
- Ok(s) => println!("合法:{s}"),
- Err(e) => println!("修補後:{}", e.into_utf8_lossy()),
- }
- }
輸出:
- hi�!
- 修補後:�A
本版新增的棄用 API
以下 API 從 Rust 1.99 起標記為棄用(deprecated),新程式不要再使用;說明欄為官方原文。
| 模組 | 項目 | 官方說明 |
|---|---|---|
std | i128::max_value | replaced by the MAX associated constant on this type |
std | i128::min_value | replaced by the MIN associated constant on this type |
std | i16::max_value | replaced by the MAX associated constant on this type |
std | i16::min_value | replaced by the MIN associated constant on this type |
std | i32::max_value | replaced by the MAX associated constant on this type |
std | i32::min_value | replaced by the MIN associated constant on this type |
std | i64::max_value | replaced by the MAX associated constant on this type |
std | i64::min_value | replaced by the MIN associated constant on this type |
std | i8::max_value | replaced by the MAX associated constant on this type |
std | i8::min_value | replaced by the MIN associated constant on this type |
std | isize::max_value | replaced by the MAX associated constant on this type |
std | isize::min_value | replaced by the MIN associated constant on this type |
std | u128::max_value | replaced by the MAX associated constant on this type |
std | u128::min_value | replaced by the MIN associated constant on this type |
std | u16::max_value | replaced by the MAX associated constant on this type |
std | u16::min_value | replaced by the MIN associated constant on this type |
std | u32::max_value | replaced by the MAX associated constant on this type |
std | u32::min_value | replaced by the MIN associated constant on this type |
std | u64::max_value | replaced by the MAX associated constant on this type |
std | u64::min_value | replaced by the MIN associated constant on this type |
std | u8::max_value | replaced by the MAX associated constant on this type |
std | u8::min_value | replaced by the MIN associated constant on this type |
std | usize::max_value | replaced by the MAX associated constant on this type |
std | usize::min_value | replaced by the MIN associated constant on this type |
std::f32 | DIGITS | replaced by the DIGITS associated constant on f32 |
std::f32 | EPSILON | replaced by the EPSILON associated constant on f32 |
std::f32 | INFINITY | replaced by the INFINITY associated constant on f32 |
std::f32 | MANTISSA_DIGITS | replaced by the MANTISSA_DIGITS associated constant on f32 |
std::f32 | MAX | replaced by the MAX associated constant on f32 |
std::f32 | MAX_10_EXP | replaced by the MAX_10_EXP associated constant on f32 |
std::f32 | MAX_EXP | replaced by the MAX_EXP associated constant on f32 |
std::f32 | MIN | replaced by the MIN associated constant on f32 |
std::f32 | MIN_10_EXP | replaced by the MIN_10_EXP associated constant on f32 |
std::f32 | MIN_EXP | replaced by the MIN_EXP associated constant on f32 |
std::f32 | MIN_POSITIVE | replaced by the MIN_POSITIVE associated constant on f32 |
std::f32 | NAN | replaced by the NAN associated constant on f32 |
std::f32 | NEG_INFINITY | replaced by the NEG_INFINITY associated constant on f32 |
std::f32 | RADIX | replaced by the RADIX associated constant on f32 |
std::f64 | DIGITS | replaced by the DIGITS associated constant on f64 |
std::f64 | EPSILON | replaced by the EPSILON associated constant on f64 |
std::f64 | INFINITY | replaced by the INFINITY associated constant on f64 |
std::f64 | MANTISSA_DIGITS | replaced by the MANTISSA_DIGITS associated constant on f64 |
std::f64 | MAX | replaced by the MAX associated constant on f64 |
std::f64 | MAX_10_EXP | replaced by the MAX_10_EXP associated constant on f64 |
std::f64 | MAX_EXP | replaced by the MAX_EXP associated constant on f64 |
std::f64 | MIN | replaced by the MIN associated constant on f64 |
std::f64 | MIN_10_EXP | replaced by the MIN_10_EXP associated constant on f64 |
std::f64 | MIN_EXP | replaced by the MIN_EXP associated constant on f64 |
std::f64 | MIN_POSITIVE | replaced by the MIN_POSITIVE associated constant on f64 |
std::f64 | NAN | replaced by the NAN associated constant on f64 |
std::f64 | NEG_INFINITY | replaced by the NEG_INFINITY associated constant on f64 |
std::f64 | RADIX | replaced by the RADIX associated constant on f64 |
std::i128 | MAX | replaced by the MAX associated constant on this type |
std::i128 | MIN | replaced by the MIN associated constant on this type |
std::i16 | MAX | replaced by the MAX associated constant on this type |
std::i16 | MIN | replaced by the MIN associated constant on this type |
std::i32 | MAX | replaced by the MAX associated constant on this type |
std::i32 | MIN | replaced by the MIN associated constant on this type |
std::i64 | MAX | replaced by the MAX associated constant on this type |
std::i64 | MIN | replaced by the MIN associated constant on this type |
std::i8 | MAX | replaced by the MAX associated constant on this type |
std::i8 | MIN | replaced by the MIN associated constant on this type |
std::isize | MAX | replaced by the MAX associated constant on this type |
std::isize | MIN | replaced by the MIN associated constant on this type |
std::sync::atomic | Atomic::fetch_update | renamed to try_update for consistency |
std::u128 | MAX | replaced by the MAX associated constant on this type |
std::u128 | MIN | replaced by the MIN associated constant on this type |
std::u16 | MAX | replaced by the MAX associated constant on this type |
std::u16 | MIN | replaced by the MIN associated constant on this type |
std::u32 | MAX | replaced by the MAX associated constant on this type |
std::u32 | MIN | replaced by the MIN associated constant on this type |
std::u64 | MAX | replaced by the MAX associated constant on this type |
std::u64 | MIN | replaced by the MIN associated constant on this type |
std::u8 | MAX | replaced by the MAX associated constant on this type |
std::u8 | MIN | replaced by the MIN associated constant on this type |
std::usize | MAX | replaced by the MAX associated constant on this type |
std::usize | MIN | replaced by the MIN associated constant on this type |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
1.99.1 | 2026-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 個。官方未公布影響範圍的漏洞只列在修正它的版本,不計入其他版本。