In this first example, the goal is to build a minimal Hello World Rust binary with Bazel.
- Cargo & Rust
- C compiler (gcc, or clang)
A minimal MODULE.bazel file for Rust contains three parts:
- A module declaration with a name and version
- Loading the Rust rules
- Configuration of the Rust toolchain
Add to your MODULE.bazel file:
module(
name = "hello_world",
version = "0.0.0"
)
# https://github.com/bazelbuild/rules_rust/releases
bazel_dep(name = "rules_rust", version = "0.46.0")
# Rust toolchain
RUST_EDITION = "2021"
RUST_VERSION = "1.79.0"
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = RUST_EDITION,
versions = [RUST_VERSION],
)
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")
Next, you declare your Rust binary target in a BUILD file.
load("@rules_rust//rust:defs.bzl", "rust_binary")
rust_binary(
name = "bin",
srcs = ["src/main.rs"],
deps = [],
visibility = ["//visibility:public"],
)
This is it. Now run your build:
bazel build //...
And run your target:
bazel run //hello_world:bin