You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-07-02 13:14:30 +02:00
Publish Comprehensive Rust 🦀
This commit is contained in:
23
src/android/build-rules/binary.md
Normal file
23
src/android/build-rules/binary.md
Normal file
@ -0,0 +1,23 @@
|
||||
# Rust Binaries
|
||||
|
||||
Let us start with a simple application. At the root of an AOSP checkout, create
|
||||
the following files:
|
||||
|
||||
_hello_rust/Android.bp_:
|
||||
|
||||
```javascript
|
||||
{{#include binary/Android.bp}}
|
||||
```
|
||||
|
||||
_hello_rust/src/main.rs_:
|
||||
|
||||
```rust
|
||||
{{#include binary/src/main.rs:main}}
|
||||
```
|
||||
|
||||
You can now build, push, and run the binary:
|
||||
|
||||
```shell
|
||||
{{#include ../build_all.sh:hello_rust}}
|
||||
Hello from Rust!
|
||||
```
|
5
src/android/build-rules/binary/Android.bp
Normal file
5
src/android/build-rules/binary/Android.bp
Normal file
@ -0,0 +1,5 @@
|
||||
rust_binary {
|
||||
name: "hello_rust",
|
||||
crate_name: "hello_rust",
|
||||
srcs: ["src/main.rs"],
|
||||
}
|
21
src/android/build-rules/binary/src/main.rs
Normal file
21
src/android/build-rules/binary/src/main.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// ANCHOR: main
|
||||
//! Rust demo.
|
||||
|
||||
/// Prints a greeting to standard output.
|
||||
fn main() {
|
||||
println!("Hello from Rust!");
|
||||
}
|
37
src/android/build-rules/library.md
Normal file
37
src/android/build-rules/library.md
Normal file
@ -0,0 +1,37 @@
|
||||
# Rust Libraries
|
||||
|
||||
You use `rust_library` to create a new Rust library for Android.
|
||||
|
||||
Here we declare a dependency on two libraries:
|
||||
|
||||
* `libgreeting`, which we define below,
|
||||
* `libtextwrap`, which is a crate already vendored in
|
||||
[`external/rust/crates/`][crates].
|
||||
|
||||
[crates]: https://cs.android.com/android/platform/superproject/+/master:external/rust/crates/
|
||||
|
||||
_hello_rust/Android.bp_:
|
||||
|
||||
```javascript
|
||||
{{#include library/Android.bp}}
|
||||
```
|
||||
|
||||
_hello_rust/src/main.rs_:
|
||||
|
||||
```rust,ignore
|
||||
{{#include library/src/main.rs:main}}
|
||||
```
|
||||
|
||||
_hello_rust/src/lib.rs_:
|
||||
|
||||
```rust,ignore
|
||||
{{#include library/src/lib.rs:greeting}}
|
||||
```
|
||||
|
||||
You build, push, and run the binary like before:
|
||||
|
||||
```shell
|
||||
{{#include ../build_all.sh:hello_rust_with_dep}}
|
||||
Hello Bob, it is very
|
||||
nice to meet you!
|
||||
```
|
16
src/android/build-rules/library/Android.bp
Normal file
16
src/android/build-rules/library/Android.bp
Normal file
@ -0,0 +1,16 @@
|
||||
rust_binary {
|
||||
name: "hello_rust_with_dep",
|
||||
crate_name: "hello_rust_with_dep",
|
||||
srcs: ["src/main.rs"],
|
||||
rustlibs: [
|
||||
"libgreetings",
|
||||
"libtextwrap",
|
||||
],
|
||||
prefer_rlib: true,
|
||||
}
|
||||
|
||||
rust_library {
|
||||
name: "libgreetings",
|
||||
crate_name: "greetings",
|
||||
srcs: ["src/lib.rs"],
|
||||
}
|
21
src/android/build-rules/library/src/lib.rs
Normal file
21
src/android/build-rules/library/src/lib.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// ANCHOR: greeting
|
||||
//! Greeting library.
|
||||
|
||||
/// Greet `name`.
|
||||
pub fn greeting(name: &str) -> String {
|
||||
format!("Hello {name}, it is very nice to meet you!")
|
||||
}
|
24
src/android/build-rules/library/src/main.rs
Normal file
24
src/android/build-rules/library/src/main.rs
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// ANCHOR: main
|
||||
//! Rust demo.
|
||||
|
||||
use greetings::greeting;
|
||||
use textwrap::fill;
|
||||
|
||||
/// Prints a greeting to standard output.
|
||||
fn main() {
|
||||
println!("{}", fill(&greeting("Bob"), 24));
|
||||
}
|
Reference in New Issue
Block a user