Mulai 27 Maret 2025, sebaiknya gunakan android-latest-release
, bukan aosp-main
, untuk mem-build dan berkontribusi pada AOSP. Untuk mengetahui informasi selengkapnya, lihat Perubahan pada AOSP.
Contoh Hello Rust
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Berikut adalah contoh pembuatan biner Rust yang bergantung pada
library Rust.
Modul Rust saat ini dibatasi untuk direktori tertentu yang ditentukan di
build/soong/rust/config/allowed_list.go.
Modul helloWorld
berikut menggunakan direktori external/rust
untuk menghindari
perubahan daftar ini.
Dari root sumber AOSP
mkdir -p external/rust/hello_rust/src/
mkdir -p external/rust/libsimple_printer/src/
Buat external/rust/hello_rust/src/hello_rust.rs
dengan konten berikut:
use simple_printer;
fn main() {
simple_printer::print_hello_rust();
}
Buat file external/rust/libsimple_printer/src/lib.rs
dengan konten berikut:
pub fn print_hello_rust() {
println!("Hello Rust!");
}
Buat file external/rust/hello_rust/Android.bp
dengan konten berikut:
rust_binary {
name: "hello_rust",
// srcs must contain a single source file, the entry point source.
srcs: ["src/hello_rust.rs"],
// rustlibs are Rust library dependencies. The type, rlib (static) or dylib
// (dynamic), are chosen automatically based on module type and target
// to avoid linkage issues.
rustlibs: ["libsimple_printer"],
// define any additional compilation flags
flags: [
"-C debug-assertions=yes",
],
// cc libraries can be linked in to rust binaries and libraries through the
// shared_libs and static_libs properties. These are not needed for this
// simple example, so they are not included.
}
Buat file external/rust/libsimple_printer/Android.bp
dengan konten berikut:
// rust_library provides dylib and rlib variants.
rust_library {
//name or stem properties must be of the form lib<crate_name><any_suffix>
name: "libsimple_printer",
//crate_name must match the name used in source (e.g. extern crate <name>)
crate_name: "simple_printer",
//srcs must contain a single source file, the entry point source
srcs: ["src/lib.rs"],
}
Terakhir, build modul hello_rust
Anda:
source build/envsetup.sh
lunch aosp_arm64-eng
m hello_rust
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-07-27 UTC."],[],[],null,["# Hello Rust example\n\nThe following is an example of constructing a Rust binary that depends on\na Rust library.\n\nRust modules are currently confined to specific directories defined in\n[build/soong/rust/config/allowed_list.go](https://android.googlesource.com/platform/build/soong/+/41461f36604948ef72855d56093d8b3e45f1d19c/rust/config/allowed_list.go).\nThe following `helloWorld` module uses the `external/rust` directory to avoid\nmodifying this list.\n\nFrom the AOSP source root \n\n mkdir -p external/rust/hello_rust/src/\n mkdir -p external/rust/libsimple_printer/src/\n\nCreate `external/rust/hello_rust/src/hello_rust.rs` with the following contents: \n\n use simple_printer;\n fn main() {\n simple_printer::print_hello_rust();\n }\n\nCreate `external/rust/libsimple_printer/src/lib.rs` file with the following contents: \n\n pub fn print_hello_rust() {\n println!(\"Hello Rust!\");\n }\n\nCreate an `external/rust/hello_rust/Android.bp` file with the following contents: \n\n rust_binary {\n name: \"hello_rust\",\n\n // srcs must contain a single source file, the entry point source.\n srcs: [\"src/hello_rust.rs\"],\n\n // rustlibs are Rust library dependencies. The type, rlib (static) or dylib\n // (dynamic), are chosen automatically based on module type and target\n // to avoid linkage issues.\n rustlibs: [\"libsimple_printer\"],\n\n // define any additional compilation flags\n flags: [\n \"-C debug-assertions=yes\",\n ],\n\n // cc libraries can be linked in to rust binaries and libraries through the\n // shared_libs and static_libs properties. These are not needed for this\n // simple example, so they are not included.\n }\n\nCreate an `external/rust/libsimple_printer/Android.bp` file with the following contents: \n\n // rust_library provides dylib and rlib variants.\n rust_library {\n //name or stem properties must be of the form lib\u003ccrate_name\u003e\u003cany_suffix\u003e\n name: \"libsimple_printer\",\n\n //crate_name must match the name used in source (e.g. extern crate \u003cname\u003e)\n crate_name: \"simple_printer\",\n\n //srcs must contain a single source file, the entry point source\n srcs: [\"src/lib.rs\"],\n }\n\nFinally, build your `hello_rust` module: \n\n source build/envsetup.sh\n lunch aosp_arm64-eng\n m hello_rust"]]