Rust rules
The core Rust rules for building and testing libraries, binaries, and procedural macros. Each rule is documented on its own page; this page is an overview of what each rule does and when to use it.
All rules are loadable from @rules_rust//rust:defs.bzl or from their individual
.bzl file (e.g. @rules_rust//rust:rust_binary.bzl). Loading from the
individual file is preferred for new code.
Core rules
The rules below are the primary entry points for compiling Rust code. They map directly to the crate types Cargo produces.
- rust_binary — Build a Rust executable (
--crate-type=bin). - rust_library — Build an
rlib(--crate-type=lib) that other Rust targets can depend on. - rust_static_library — Build a
staticlib(--crate-type=staticlib) for linking Rust code into a C/C++ binary. - rust_cdylib_library — Build a
cdylib(--crate-type=cdylib) for use as a shared library from C/C++ or other languages. - rust_dylib_library — Build a
dylib(--crate-type=dylib) for use as a shared library with the unstable Rust ABI. - rust_shared_library — Convenience alias over
rust_cdylib_library. - rust_proc_macro — Build a procedural macro crate
(
--crate-type=proc-macro) that other Rust targets can consume as a compile-time plugin. - rust_test — Compile and run a Rust test binary. Can wrap a
rust_library/rust_binary(viacrate = ...) to run its inline#[test]s, or compile a standalone test binary from its ownsrcs.
Additional rules
Non-core rules that ship in the same ruleset:
- rust_library_group — Group several
rust_librarytargets so they can be depended on as a single unit without generating an intermediaterlib. - rust_lint_config — Declare a reusable set of
rustc,clippy, andrustdoclint levels that can be attached to other rules via theirlint_configattribute. - rust_test_suite — Convenience macro that expands into
one
rust_testper source file, sharing a common set of dependencies. Useful fortests/**.rslayouts that mirror Cargo's integration-test directory.
Related pages
Rules that layer on top of the core rules live on their own pages:
- rustdoc — Generate and test Rust documentation.
- clippy — Run Clippy over Rust targets.
- rustfmt — Run rustfmt over Rust targets.
- unpretty — Emit
rustc -Zunpretty=...output for a crate. - cargo —
cargo_build_scriptand related Cargo-compatibility rules. - rust_analyzer — Generate a
rust-project.jsonfile for the rust-analyzer IDE server. - Rust Toolchains — Declare and register Rust toolchains.