Skip to content

Commit a190c4b

Browse files
authored
Update documentation (#4345)
* Update documentation This commit includes changes to the readme, cargo-fmt, and rustfmt - Readme - rearrange sections to an order I believe is a more natural progression (this is opinionated, open to suggestions) - add a TOC (I can add a CI test to ensure it's up to date, lmk what you think) - cargo-fmt - link to config options - rustfmt - elaborate on operation with files/stdin - include examples - link to config options Closes #4336 * fixup! Update documentation * fixup! Update documentation * fixup! Update documentation
1 parent a10434b commit a190c4b

File tree

3 files changed

+105
-68
lines changed

3 files changed

+105
-68
lines changed

README.md

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# rustfmt [![Linux badge][linux-build-status]][linux-build] [![Mac badge][mac-build-status]][mac-build] [![Windows badge][windows-build-status]][windows-build] [![crates.io badge][cratesio-badge]][cratesio-package] [![Travis config badge][travis-config-badge]][travis-config-job]
22

3+
<!-- To update: doctoc README.md --notitle -->
4+
<!-- https://github.com/thlorenz/doctoc -->
5+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
6+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7+
8+
9+
- [Quick start](#quick-start)
10+
- [On the Stable toolchain](#on-the-stable-toolchain)
11+
- [On the Nightly toolchain](#on-the-nightly-toolchain)
12+
- [Installing from source](#installing-from-source)
13+
- [Running](#running)
14+
- [Configuring Rustfmt](#configuring-rustfmt)
15+
- [Rust's Editions](#rusts-editions)
16+
- [Limitations](#limitations)
17+
- [Running Rustfmt from your editor](#running-rustfmt-from-your-editor)
18+
- [Checking style on a CI server](#checking-style-on-a-ci-server)
19+
- [How to build and test](#how-to-build-and-test)
20+
- [Tips](#tips)
21+
- [License](#license)
22+
23+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
24+
325
A tool for formatting Rust code according to style guidelines.
426

527
If you'd like to help out (and you should, it's a fun project!), see
@@ -45,42 +67,7 @@ To run on a cargo project in the current working directory:
4567
cargo +nightly fmt
4668
```
4769

48-
## Limitations
49-
50-
Rustfmt tries to work on as much Rust code as possible. Sometimes, the code
51-
doesn't even need to compile! As we approach a 1.0 release we are also looking
52-
to limit areas of instability; in particular, post-1.0, the formatting of most
53-
code should not change as Rustfmt improves. However, there are some things that
54-
Rustfmt can't do or can't do well (and thus where formatting might change
55-
significantly, even post-1.0). We would like to reduce the list of limitations
56-
over time.
57-
58-
The following list enumerates areas where Rustfmt does not work or where the
59-
stability guarantees do not apply (we don't make a distinction between the two
60-
because in the future Rustfmt might work on code where it currently does not):
61-
62-
* a program where any part of the program does not parse (parsing is an early
63-
stage of compilation and in Rust includes macro expansion).
64-
* Macro declarations and uses (current status: some macro declarations and uses
65-
are formatted).
66-
* Comments, including any AST node with a comment 'inside' (Rustfmt does not
67-
currently attempt to format comments, it does format code with comments inside, but that formatting may change in the future).
68-
* Rust code in code blocks in comments.
69-
* Any fragment of a program (i.e., stability guarantees only apply to whole
70-
programs, even where fragments of a program can be formatted today).
71-
* Code containing non-ascii unicode characters (we believe Rustfmt mostly works
72-
here, but do not have the test coverage or experience to be 100% sure).
73-
* Bugs in Rustfmt (like any software, Rustfmt has bugs, we do not consider bug
74-
fixes to break our stability guarantees).
75-
76-
77-
## Installation
78-
79-
```sh
80-
rustup component add rustfmt
81-
```
82-
83-
## Installing from source
70+
### Installing from source
8471

8572
To install from source (nightly required), first checkout to the tag or branch for the version of rustfmt you want.
8673

@@ -102,25 +89,75 @@ cargo install --path . --force --locked --features rustfmt,cargo-fmt
10289
This will install `rustfmt` in your `~/.cargo/bin`. Make sure to add `~/.cargo/bin` directory to
10390
your PATH variable.
10491

105-
10692
## Running
10793

94+
You can run `rustfmt --help` for information about available arguments.
95+
10896
You can run Rustfmt by just typing `rustfmt filename` if you used `cargo
10997
install`. This runs rustfmt on the given file, if the file includes out of line
11098
modules, then we reformat those too. So to run on a whole module or crate, you
11199
just need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also
112100
read data from stdin. Alternatively, you can use `cargo fmt` to format all
113101
binary and library targets of your crate.
114102

115-
You can run `rustfmt --help` for information about available arguments.
116-
117103
When running with `--check`, Rustfmt will exit with `0` if Rustfmt would not
118104
make any formatting changes to the input, and `1` if Rustfmt would make changes.
119105
In other modes, Rustfmt will exit with `1` if there was some error during
120106
formatting (for example a parsing or internal error) and `0` if formatting
121107
completed without error (whether or not changes were made).
122108

109+
## Configuring Rustfmt
110+
111+
Rustfmt is designed to be very configurable. You can create a TOML file called
112+
`rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent
113+
directory and it will apply the options in that file. See `rustfmt
114+
--help=config` for the options which are available, or if you prefer to see
115+
visual style previews, [GitHub page](https://rust-lang.github.io/rustfmt/).
116+
117+
By default, Rustfmt uses a style which conforms to the [Rust style guide][style
118+
guide] that has been formalized through the [style RFC
119+
process][fmt rfcs].
120+
121+
Configuration options are either stable or unstable. Stable options can always
122+
be used on any channel. Unstable options are always available on nightly, but can only be used on stable and beta with an explicit opt-in (starting in Rustfmt v2.0).
123+
124+
Unstable options are not available on stable/beta with Rustfmt v1.x.
125+
126+
See the configuration documentation on the Rustfmt [GitHub page](https://rust-lang.github.io/rustfmt/) for details (look for the `unstable_features` section).
127+
128+
### Rust's Editions
129+
130+
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
131+
executed through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition
132+
needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
133+
134+
## Limitations
135+
136+
Rustfmt tries to work on as much Rust code as possible. Sometimes, the code
137+
doesn't even need to compile! As we approach a 1.0 release we are also looking
138+
to limit areas of instability; in particular, post-1.0, the formatting of most
139+
code should not change as Rustfmt improves. However, there are some things that
140+
Rustfmt can't do or can't do well (and thus where formatting might change
141+
significantly, even post-1.0). We would like to reduce the list of limitations
142+
over time.
123143

144+
The following list enumerates areas where Rustfmt does not work or where the
145+
stability guarantees do not apply (we don't make a distinction between the two
146+
because in the future Rustfmt might work on code where it currently does not):
147+
148+
* a program where any part of the program does not parse (parsing is an early
149+
stage of compilation and in Rust includes macro expansion).
150+
* Macro declarations and uses (current status: some macro declarations and uses
151+
are formatted).
152+
* Comments, including any AST node with a comment 'inside' (Rustfmt does not
153+
currently attempt to format comments, it does format code with comments inside, but that formatting may change in the future).
154+
* Rust code in code blocks in comments.
155+
* Any fragment of a program (i.e., stability guarantees only apply to whole
156+
programs, even where fragments of a program can be formatted today).
157+
* Code containing non-ascii unicode characters (we believe Rustfmt mostly works
158+
here, but do not have the test coverage or experience to be 100% sure).
159+
* Bugs in Rustfmt (like any software, Rustfmt has bugs, we do not consider bug
160+
fixes to break our stability guarantees).
124161

125162
## Running Rustfmt from your editor
126163

@@ -131,7 +168,6 @@ completed without error (whether or not changes were made).
131168
* Visual Studio Code using [vscode-rust](https://github.com/editor-rs/vscode-rust), [vsc-rustfmt](https://github.com/Connorcpu/vsc-rustfmt) or [rls_vscode](https://github.com/jonathandturner/rls_vscode) through RLS.
132169
* [IntelliJ or CLion](intellij.md)
133170

134-
135171
## Checking style on a CI server
136172

137173
To keep your code base consistently formatted, it can be helpful to fail the CI build
@@ -197,32 +233,6 @@ CFG_RELEASE_CHANNEL=nightly CFG_RELEASE=1.45.0-nightly cargo test --all-features
197233
To run rustfmt after this, use `cargo run --bin rustfmt -- filename`. See the
198234
notes above on running rustfmt.
199235

200-
201-
## Configuring Rustfmt
202-
203-
Rustfmt is designed to be very configurable. You can create a TOML file called
204-
`rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent
205-
directory and it will apply the options in that file. See `rustfmt
206-
--help=config` for the options which are available, or if you prefer to see
207-
visual style previews, [GitHub page](https://rust-lang.github.io/rustfmt/).
208-
209-
By default, Rustfmt uses a style which conforms to the [Rust style guide][style
210-
guide] that has been formalized through the [style RFC
211-
process][fmt rfcs].
212-
213-
Configuration options are either stable or unstable. Stable options can always
214-
be used on any channel. Unstable options are always available on nightly, but can only be used on stable and beta with an explicit opt-in (starting in Rustfmt v2.0).
215-
216-
Unstable options are not available on stable/beta with Rustfmt v1.x.
217-
218-
See the configuration documentation on the Rustfmt [GitHub page](https://rust-lang.github.io/rustfmt/) for details (look for the `unstable_features` section).
219-
220-
### Rust's Editions
221-
222-
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
223-
executed through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition
224-
needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
225-
226236
## Tips
227237

228238
* For things you do not want rustfmt to mangle, use `#[rustfmt::skip]`

src/cargo-fmt/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub struct Opts {
4747
message_format: Option<String>,
4848

4949
/// Options passed to rustfmt
50+
///
51+
/// To see all rustfmt options, run `rustfmt --help'.
52+
/// To see all rustfmt configuration options, please visit https://rust-lang.github.io/rustfmt.
5053
// 'raw = true' to make `--` explicit.
5154
#[structopt(name = "rustfmt_options", raw(true))]
5255
rustfmt_options: Vec<String>,

src/rustfmt/main.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,30 @@ fn main() {
4343

4444
/// Format Rust code
4545
#[derive(Debug, StructOpt, Clone)]
46-
#[structopt(name = "rustfmt", version = include_str!(concat!(env!("OUT_DIR"),"/version-info.txt")))]
46+
#[structopt(
47+
name = "rustfmt",
48+
version = include_str!(concat!(env!("OUT_DIR"),"/version-info.txt")),
49+
about = r#"Format Rust code
50+
51+
Rustfmt runs on a set of files or stdin. When invoked without any file arguments, rustfmt will
52+
read code from stdin.
53+
54+
Please visit https://rust-lang.github.io/rustfmt to see all rustfmt configuration options.
55+
56+
EXAMPLES
57+
58+
cat lib.rs | rustfmt
59+
Feed the contents of "lib.rs" to rustfmt via stdin
60+
61+
rustfmt lib.rs main.rs
62+
Run rustfmt over "lib.rs" and "main.rs", formatting in-place.
63+
64+
rustfmt --emit=stdout lib.rs main.rs
65+
Run rustfmt over "lib.rs" and "main.rs", writing to stdout (rather than in-place)
66+
67+
rustfmt --config-path=rustfmt.toml --print-config=current
68+
Print the resolved rustfmt configuration formed by rustfmt.toml
69+
"#)]
4770
struct Opt {
4871
/// Run in 'check' mode.
4972
///
@@ -60,7 +83,8 @@ struct Opt {
6083
config_path: Option<PathBuf>,
6184
/// Rust compiler edition
6285
///
63-
/// Specify which edition of the compiler to use when formatting code.
86+
/// Specify which edition of the compiler to use when formatting code. This behaves identically
87+
/// to the "edition" configuration option.
6488
#[structopt(long, name = "2015|2018")]
6589
edition: Option<Edition>,
6690
/// Print configuration options.

0 commit comments

Comments
 (0)