# Rust Rules for Bazel ## Overview These build rules are used for building [Rust][rust] projects with Bazel. * [Setup](#setup) * [Basic Example](#basic-example) * [Build Rule Reference](#reference) * [`rust_library`](#reference-rust_library) * [`rust_binary`](#reference-rust_binary) * [`rust_test`](#reference-rust_test) * [Roadmap](#roadmap) [rust]: http://www.rust-lang.org/ ## Setup To use the Rust rules, simply copy the contents of `rust.WORKSPACE` to your `WORKSPACE` file. ## Basic Example Suppose you have the following directory structure for a simple Rust library crate: ``` [workspace]/ WORKSPACE hello_lib/ BUILD src/ greeter.rs lib.rs ``` `hello_lib/src/greeter.rs`: ```rust pub struct Greeter { greeting: String, } impl Greeter { pub fn new(greeting: &str) -> Greeter { Greeter { greeting: greeting.to_string(), } } pub fn greet(&self, thing: &str) { println!("{} {}", &self.greeting, thing); } } ``` `hello_lib/src/lib.rs`: ```rust pub mod greeter; ``` `hello_lib/BUILD`: ```python package(default_visibility = ["//visibility:public"]) load("/tools/build_rules/rust/rust", "rust_library") rust_library( name = "hello_lib", srcs = [ "src/greeter.rs", "src/lib.rs", ], ) ``` Build the library: ``` $ bazel build //hello_lib INFO: Found 1 target... Target //examples/rust/hello_lib:hello_lib up-to-date: bazel-bin/examples/rust/hello_lib/libhello_lib.rlib INFO: Elapsed time: 1.245s, Critical Path: 1.01s ``` Now, let's add a binary crate that uses the `hello_lib` library. The directory structure now looks like the following: ``` [workspace]/ WORKSPACE hello_lib/ BUILD src/ greeter.rs lib.rs hello_world/ BUILD src/ main.rs ``` `hello_world/src/main.rs`: ```rust extern crate hello_lib; use hello_lib::greeter; fn main() { let hello = greeter::Greeter::new("Hello"); hello.greet("world"); } ``` `hello_world/BUILD`: ```python load("/tools/build_rules/rust/rust", "rust_binary") rust_binary( name = "hello_world", srcs = ["src/main.rs"], deps = ["//hello_lib"], ) ``` Build and run `hello_world`: ``` $ bazel run //hello_world INFO: Found 1 target... Target //examples/rust/hello_world:hello_world up-to-date: bazel-bin/examples/rust/hello_world/hello_world INFO: Elapsed time: 1.308s, Critical Path: 1.22s INFO: Running command line: bazel-bin/examples/rust/hello_world/hello_world Hello world ``` ## Build Rule Reference ### `rust_library` `rust_library(name, srcs, deps, data, crate_features, rustc_flags)`
Attribute | Description |
---|---|
name |
Name, required
A unique name for this rule. This name will also be used as the name of the library crate built by this rule. |
srcs |
List of labels, required
List of Rust
There must be a file either named |
deps |
List of labels, optional
List of other libraries to be linked to this library target.
These can be either other |
data |
List of labels, optional
List of files used by this rule at runtime.
This attribute can be used to specify any data files that are embedded
into the library, such as via the
|
crate_features |
List of strings, optional
List of features to enable for this crate.
Features are defined in the code using the
|
rustc_flags |
List of strings, optional
List of compiler flags passed to |
Attribute | Description |
---|---|
name |
Name, required
A unique name for this rule. This name will also be used as the name of the binary crate built by this rule. |
srcs |
List of labels, required
List of Rust
There must be a file either named |
deps |
List of labels, optional
List of other libraries to be linked to this library target.
These must be |
data |
List of labels, optional
List of files used by this rule at runtime.
This attribute can be used to specify any data files that are embedded
into the library, such as via the
|
crate_features |
List of strings, optional
List of features to enable for this crate.
Features are defined in the code using the
|
rustc_flags |
List of strings, optional
List of compiler flags passed to |
Attribute | Description |
---|---|
name |
Name, required
A unique name for this rule. This name will also be used as the name of the binary test crate built by this rule. |
srcs |
List of labels, required
List of Rust
There must be a file either with a name matching the name of this
test. For example, if the name of a |
deps |
List of labels, optional
List of other libraries to be linked to this test target.
These must be |
data |
List of labels, optional
List of files used by this rule at runtime.
This attribute can be used to specify any data files that are embedded
into the library, such as via the
|
crate_features |
List of strings, optional
List of features to enable for this crate.
Features are defined in the code using the
|
rustc_flags |
List of strings, optional
List of compiler flags passed to |