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 (via crate = ...) to run its inline #[test]s, or compile a standalone test binary from its own srcs.

Additional rules

Non-core rules that ship in the same ruleset:

  • rust_library_group — Group several rust_library targets so they can be depended on as a single unit without generating an intermediate rlib.
  • rust_lint_config — Declare a reusable set of rustc, clippy, and rustdoc lint levels that can be attached to other rules via their lint_config attribute.
  • rust_test_suite — Convenience macro that expands into one rust_test per source file, sharing a common set of dependencies. Useful for tests/**.rs layouts that mirror Cargo's integration-test directory.

Rules that layer on top of the core rules live on their own pages: