自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Android Rust 簡介
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 平台支援在 Rust 中開發原生 OS 元件,Rust 是一種新型的系統程式設計語言,可提供記憶體安全保證,效能與 C/C++ 相當。Rust 會同時使用編譯時檢查 (可強制執行物件生命週期和擁有權) 和執行階段檢查 (可確保有效的記憶體存取),因此不需要垃圾收集器。
Rust 提供多種現代語言功能,可讓開發人員更有效率地編寫程式碼,並對程式碼更有信心:
- 安全的並行程式設計:這項功能可讓使用者輕鬆編寫高效、執行緒安全的程式碼,因此 Rust 才會提出「無懼並行」的口號。
- 具表達力的型別系統:Rust 允許使用具表達力的型別 (例如 Newtype 包裝函式和含有內容的列舉變數),有助於避免邏輯程式設計錯誤。
- 更強大的編譯時檢查功能:在編譯時偵測到的錯誤越多,開發人員就越有信心,認為在程式碼成功編譯後,程式碼會按照預期運作。
- 內建測試架構:Rust 提供內建測試架構,可將單元測試與測試的實作項目一併放置,讓單元測試更容易納入。
- 錯誤處理強制執行:含有可復原失敗的函式可傳回結果類型,這會是成功變化版本或錯誤變化版本。編譯器要求呼叫端檢查並處理從函式呼叫傳回的
Result
列舉的錯誤變化版本。這麼做可減少未處理的失敗情況所導致的錯誤。
- 初始化:Rust 要求每個變數在使用前都必須初始化為其類型的合法成員,以免不小心將初始化值設為不安全的值。
- 更安全的整數處理方式:所有整數型轉換都是明確轉換。開發人員在將值指派給變數,或嘗試使用其他類型的算術運算時,不會在函式呼叫期間意外轉換。根據預設,Android for Rust 會啟用溢位檢查功能,這項功能要求溢位運算必須明確指定。
如需更多資訊,請參閱一系列有關 Android Rust 支援的網誌文章:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Android Rust introduction\n\nThe Android platform provides support for developing native OS components in Rust,\na modern systems-programming language that provides memory safety guarantees with\nperformance equivalent to C/C++. Rust uses a combination of compile-time checks\nthat enforce object lifetime and ownership, and runtime checks that ensure valid\nmemory accesses, thereby eliminating the need for a garbage collector.\n\nRust provides a range of modern language features which allow developers to be\nmore productive and confident in their code:\n\n- **Safe concurrent programming** - The ease with which this allows users to write efficient, thread-safe code has given rise to Rust's [Fearless Concurrency](https://doc.rust-lang.org/book/ch16-00-concurrency.html) slogan.\n- **Expressive type system** - Rust helps prevent logical programming bugs by allowing for highly expressive types (such as Newtype wrappers, and enum variants with contents).\n- **Stronger Compile-time Checks** - More bugs caught at compile-time increases developer confidence that when code compiles successfully, it works as intended.\n- **Built-in Testing Framework** - Rust provides a built-in testing framework where unit tests can be placed alongside the implementation they test, making unit testing easier to include.\n- **Error handling enforcement** - Functions with recoverable failures can return a [Result type](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html), which will be either a success variant or an error variant. The compiler requires callers to check for and handle the error variant of a `Result` enum returned from a function call. This reduces the potential for bugs resulting from unhandled failures.\n- **Initialization** - Rust requires every variable to be initialized to a legal member of its type before use, preventing an unintentional initialization to an unsafe value.\n- **Safer integer handling** - All integer-type conversions are explicit casts. Developers can't accidentally cast during a function call when assigning to a variable, or when attempting to do arithmetic with other types. Overflow checking is on by default in Android for Rust, which requires overflow operations to be explicit.\n\nFor more information, see the series of blog posts on Android Rust support:\n\n- [Rust in the Android Platform](https://security.googleblog.com/2021/04/rust-in-android-platform.html) \n Provides an overview on why the Android team introduced Rust as a new platform language.\n- [Integrating Rust into the Android Open Source Project](https://security.googleblog.com/2021/05/integrating-rust-into-android-open.html) \n Discusses how Rust support has been introduced to the build system, and why certain design decisions were made.\n- [Rust/C++ interop in the Android Platform](https://security.googleblog.com/2021/06/rustc-interop-in-android-platform.html) \n Discusses the approach to Rust/C++ interoperability within Android."]]