Build rules exercise
In your Chromium build, add a new Rust target to //ui/base/BUILD.gn
containing:
#![allow(unused)] fn main() { #[no_mangle] pub extern "C" fn hello_from_rust() { println!("Hello from Rust!") } }
Important: note that no_mangle
here is considered a type of unsafety by the Rust compiler, so youâll need to to allow unsafe code in your gn
target.
Add this new Rust target as a dependency of //ui/base:base
. Declare this function at the top of ui/base/resource/resource_bundle.cc
(later, weâll see how this can be automated by bindings generation tools):
extern "C" void hello_from_rust();
Call this function from somewhere in ui/base/resource/resource_bundle.cc
- we suggest the top of ResourceBundle::MaybeMangleLocalizedString
. Build and run Chromium, and ensure that âHello from Rust!â is printed lots of times.
If you use VSCode, now set up Rust to work well in VSCode. It will be useful in subsequent exercises. If youâve succeeded, you will be able to use right-click âGo to definitionâ on println!
.
Where to find help
- The options available to the
rust_static_library
gn template - Information about
#[no_mangle]
- Information about
extern "C"
- Information about gnâs
--export-rust-project
switch - How to install rust-analyzer in VSCode
This example is unusual because it boils down to the lowest-common-denominator interop language, C. Both C++ and Rust can natively declare and call C ABI functions. Later in the course, weâll connect C++ directly to Rust.
allow_unsafe = true
is required here because #[no_mangle]
might allow Rust to generate two functions with the same name, and Rust can no longer guarantee that the right one is called.
If you need a pure Rust executable, you can also do that using the rust_executable
gn template.