Rules rust

Overview

This repository provides rules for building Rust projects with Bazel.

Setup

To use the Rust rules, add the following to your WORKSPACE file to add the external repositories for the Rust toolchain:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# To find additional information on this release or newer ones visit:
# https://github.com/bazelbuild/rules_rust/releases
http_archive(
    name = "rules_rust",
    sha256 = "7fb9b4fe1a6fb4341bdf7c623e619460ecc0f52d5061cc56abc750111fba8a87",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/0.7.0/rules_rust-v0.7.0.tar.gz",
        "https://github.com/bazelbuild/rules_rust/releases/download/0.7.0/rules_rust-v0.7.0.tar.gz",
    ],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

The rules are under active development, as such the lastest commit on the main branch should be used. main is only tested against 4.0.0 as the minimum supported version of Bazel. Though previous versions may still be functional in certain environments.

Rules

You can also browse the full API in one page.

Experimental rules

Specifying Rust version

To build with a particular version of the Rust compiler, pass that version to rust_register_toolchains:

rust_register_toolchains(version = "1.59.0", edition="2018")

As well as an exact version, version can be set to "nightly" or "beta". If set to these values, iso_date must also be set:

rust_register_toolchains(version = "nightly", iso_date = "2022-02-23", edition="2018")

Similarly, rustfmt_version may also be configured:

rust_register_toolchains(rustfmt_version = "1.59.0")

External Dependencies

crate_universe is a tool built into rules_rust that can be used to fetch dependencies. Additionally, cargo-raze is an older third-party which can also fetch dependencies.