Back to Comprehensive Rust

A Simple Rust Library

src/android/interoperability/with-c/rust-library.md

latest716 B
Original Source
<!-- Copyright 2025 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

A Simple Rust Library

Exporting Rust functions and types to C is easy. Here's a simple Rust library:

interoperability/rust/libanalyze/analyze.rs

rust,editable
# // Copyright 2025 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include rust/libanalyze/analyze.rs:analyze_numbers}}

interoperability/rust/libanalyze/Android.bp

javascript
{{#include rust/libanalyze/Android.bp}}
<details>

#[unsafe(no_mangle)] disables Rust's usual name mangling, so the exported symbol will just be the name of the function. You can also use #[unsafe(export_name = "some_name")] to specify whatever name you want.

</details>