自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Android Rust 简介
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 平台支持在 Rust 中开发原生操作系统组件。Rust 是一种现代系统编程语言,不仅能提供可媲美 C/C++ 的性能,还可以保证内存安全。Rust 结合使用编译时检查和运行时检查,编译时检查用于执行对象生命周期和所有权检查,运行时检查则可以确保内存访问有效,因此 Rust 根本不需要垃圾回收器。
Rust 提供了一系列现代语言功能,让开发者在提高工作效率的同时,对自己编写的代码更有信心:
- 安全的并发编程 - 此功能可以让用户轻松编写高效、线程安全的代码,这也是 Rust 打出无畏并发口号的原因。
- 富有表现力的类型系统 - Rust 支持使用表现力极强的类型(例如 Newtype 封装容器和列出内容的枚举变体),有助于防止逻辑上的编程 bug。
- 更严格的编译时检查 - 能够在编译时捕获更多 bug,提高开发者对代码编译成功后按预期运行的信心。
- 内置测试框架 - Rust 提供了一个内置测试框架,在该框架中,可以将单元测试与其测试的实现放在一起,因此更容易在测试中包含单元测试。
- 错误处理强制执行 - 产生可恢复错误的函数可以返回 Result 类型,它是一个成功变体或一个错误变体。编译器会要求调用方检查并处理从函数调用返回的
Result
枚举的错误变体。这可以降低由于未处理的错误而导致 bug 的可能性。
- 初始化 - Rust 要求每个变量必须初始化为所属类型的合法成员后才能使用,以防变量意外地初始化为不安全的值。
- 更安全的整数处理 - 所有整数类型的转换都是显式类型转换。分配给变量时或尝试与其他类型的值进行算术运算时,开发者不可能在函数调用期间意外地进行类型转换。Android 中默认启用对 Rust 的溢出检查,要求溢出操作必须是显式操作。
如需了解详情,请参阅有关 Android Rust 支持的系列博文:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-04。
[null,null,["最后更新时间 (UTC):2025-04-04。"],[],[],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."]]