नमस्ते Rust का उदाहरण

यहां Rust बाइनरी बनाने का एक उदाहरण दिया गया है, जो Rust लाइब्रेरी पर निर्भर करता है.

फ़िलहाल, रस्ट मॉड्यूल सिर्फ़ build/soong/rust/config/allowed_list.go में बताई गई खास डायरेक्ट्री तक सीमित है. इस सूची में बदलाव करने से बचने के लिए, यहां दिया गया helloWorld मॉड्यूल external/rust डायरेक्ट्री का इस्तेमाल करता है.

AOSP सोर्स रूट से

mkdir -p external/rust/hello_rust/src/
mkdir -p external/rust/libsimple_printer/src/

इस कॉन्टेंट का इस्तेमाल करके external/rust/hello_rust/src/hello_rust.rs बनाएं:

use simple_printer;
fn main() {
 simple_printer::print_hello_rust();
}

इस कॉन्टेंट के साथ external/rust/libsimple_printer/src/lib.rs फ़ाइल बनाएं:

pub fn print_hello_rust() {
  println!("Hello Rust!");
}

इस कॉन्टेंट का इस्तेमाल करके external/rust/hello_rust/Android.bp फ़ाइल बनाएं:

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.
}

इस कॉन्टेंट का इस्तेमाल करके external/rust/libsimple_printer/Android.bp फ़ाइल बनाएं:

// 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"],
}

आखिर में, अपना hello_rust मॉड्यूल बनाएं:

source build/envsetup.sh
lunch aosp_arm64-eng
m hello_rust