blob: 48bac64dd7925248b3316701c04f1ec1837e7b2c [file] [log] [blame]
Brian Silvermancc09f182022-03-09 15:40:20 -08001#[[
2## Overview
3]]#
4
5[Rustfmt][rustfmt] is a tool for formatting Rust code according to style guidelines.
6By default, Rustfmt uses a style which conforms to the [Rust style guide][rsg] that
7has been formalized through the [style RFC process][rfcp]. A complete list of all
8configuration options can be found in the [Rustfmt GitHub Pages][rgp].
9
10
11#[[
12### Setup
13]]#
14
15Formatting your Rust targets' source code requires no setup outside of loading `rules_rust`
16in your workspace. Simply run `bazel run @rules_rust//:rustfmt` to format source code.
17
Adam Snaider1c095c92023-07-08 02:09:58 -040018In addition to this formatter, a simple check can be performed using the [rustfmt_aspect](#rustfmt-aspect) aspect by running
19```text
20bazel build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect --output_groups=rustfmt_checks
21```
22
23Add the following to a `.bazelrc` file to enable this check during the build phase.
Brian Silvermancc09f182022-03-09 15:40:20 -080024
25```text
26build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
27build --output_groups=+rustfmt_checks
28```
29
30It's recommended to only enable this aspect in your CI environment so formatting issues do not
31impact user's ability to rapidly iterate on changes.
32
33The `rustfmt_aspect` also uses a `--@rules_rust//:rustfmt.toml` setting which determines the
34[configuration file][rgp] used by the formatter (`@rules_rust//tools/rustfmt`) and the aspect
35(`rustfmt_aspect`). This flag can be added to your `.bazelrc` file to ensure a consistent config
36file is used whenever `rustfmt` is run:
37
38```text
39build --@rules_rust//:rustfmt.toml=//:rustfmt.toml
40```
41
42[rustfmt]: https://github.com/rust-lang/rustfmt#readme
43[rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md
44[rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs
45[rgp]: https://rust-lang.github.io/rustfmt/