2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
Android Rust の概要
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android プラットフォームでは、Rust で OS のネイティブ コンポーネントを開発できます。Rust は最新のシステム プログラミング言語であり、C/C++ と同等のパフォーマンスで、メモリの安全性を保証します。Rust は、オブジェクトの存続期間と所有権を適用するコンパイル時チェックと、有効なメモリアクセスを確保する実行時チェックを組み合わせることで、ガベージ コレクタを必要としません。
Rust には、デベロッパーがコードの生産性を高め、自信を持ってコードを書けるように、さまざまな最新の言語機能を備えています。
- 安全な同時実行プログラミング - この機能により、ユーザーは効率的でスレッドセーフなコードを記述できるようになるため、Rust の Fearless Concurrency スローガンが生まれました。
- 表現型システム - Rust は、表現性に優れた型(Newtype ラッパー、コンテンツを含む列挙型バリアントなど)を可能にすることで、論理プログラミングのバグを防ぎます。
- コンパイル時のチェックを強化 - コンパイル時に多数のバグが検出されることで、コードが正常にコンパイルされた場合は意図したとおりに動作するというデベロッパーの信頼が高まります。
- 組み込みのテスト フレームワーク - Rust には組み込みのテスト フレームワークが用意されており、テストする実装と一緒に単体テストを配置できるため、単体テストを簡単に含めることができます。
- エラー処理の適用 - 回復可能なエラーのある関数は、成功バリアントまたはエラー バリアントのいずれかの Result 型を返すことができます。コンパイラは、関数呼び出しから返された
Result
列挙型のエラー バリアントをチェックして処理することを呼び出し元に要求します。これにより、未処理のエラーによってバグが発生する可能性が低くなります。
- 初期化 - Rust では、使用前にすべての変数をその型の正当なメンバーで初期化する必要があります。これにより、意図せずに安全でない値に初期化されるのを防ぎます。
- より安全な整数処理 - 整数型の変換はすべて明示的なキャストです。
デベロッパーは、関数への呼び出し中に変数に代入するときや、他の型で演算しようとしたときに、誤ってキャストしてしまうことがなくなります。Android で Rust を使用する場合、オーバーフローのチェックはデフォルトでオンになっています。そのため、オーバーフロー操作を明示的に指定する必要があります。
Android Rust のサポートに関してブログを公開していますので、詳しくはそちらの記事をご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-04-04 UTC。
[null,null,["最終更新日 2025-04-04 UTC。"],[],[],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."]]