Bibliotecas de Rust

Crea una biblioteca de Rust para Android con rust_library.

Aquí declaramos una dependencia en dos bibliotecas:

  • libgreeting, que definimos más abajo.
  • libtextwrap, que es un crate ya incluido en external/rust/crates/.

hello_rust/Android.bp:

rust_binary { name: "hello_rust_with_dep", crate_name: "hello_rust_with_dep", srcs: ["src/main.rs"], rustlibs: [ "libgreetings", "libtextwrap", ], prefer_rlib: true, // Es necesario para evitar errores de enlace dinámico. } rust_library { name: "libgreetings", crate_name: "greetings", srcs: ["src/lib.rs"], }

hello_rust/src/main.rs:

//! Demo de Rust. use greetings::greeting; use textwrap::fill; /// Imprime un saludo en una salida estándar. fn main() { println!("{}", fill(&greeting("Bob"), 24)); }

hello_rust/src/lib.rs:

//! Biblioteca de saludos. /// Saluda a `name`. pub fn greeting(name: &str) -> String { format!("Hello {name}, it is very nice to meet you!") }

Puedes compilar, insertar y ejecutar el binario como antes:

m hello_rust_with_dep adb push "$ANDROID_PRODUCT_OUT/system/bin/hello_rust_with_dep" /data/local/tmp adb shell /data/local/tmp/hello_rust_with_dep
Hello Bob, it is very nice to meet you!