Skip to content

Commit e91edf5

Browse files
authored
Update documentation, round 2 (#4361)
* Update documentation, round 2 This time we try to clean up a lot of the README, after its structure was reorged in #4345. cc @calebcartwright Closes #4336 * fixup! Update documentation, round 2
1 parent 0921e1f commit e91edf5

File tree

1 file changed

+66
-38
lines changed

1 file changed

+66
-38
lines changed

Diff for: README.md

+66-38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
- [On the Stable toolchain](#on-the-stable-toolchain)
1111
- [On the Nightly toolchain](#on-the-nightly-toolchain)
1212
- [Installing from source](#installing-from-source)
13-
- [Running](#running)
13+
- [Usage](#usage)
14+
- [Running `cargo fmt`](#running-cargo-fmt)
15+
- [Running `rustfmt` directly](#running-rustfmt-directly)
16+
- [Verifying code is formatted](#verifying-code-is-formatted)
17+
- [Exit codes](#exit-codes)
1418
- [Configuring Rustfmt](#configuring-rustfmt)
19+
- [Differences in rustfmt versions](#differences-in-rustfmt-versions)
20+
- [Default formatting of submodules](#default-formatting-of-submodules)
21+
- [Construction of config options](#construction-of-config-options)
1522
- [Rust's Editions](#rusts-editions)
1623
- [Limitations](#limitations)
1724
- [Running Rustfmt from your editor](#running-rustfmt-from-your-editor)
@@ -69,7 +76,7 @@ cargo +nightly fmt
6976

7077
### Installing from source
7178

72-
To install from source (nightly required), first checkout to the tag or branch for the version of rustfmt you want.
79+
To install from source (nightly required), first checkout to the tag or branch for the version of rustfmt you want.
7380

7481
The easiest way to install is via [cargo make][cargo-make]
7582

@@ -80,28 +87,43 @@ cargo make install
8087
Alternatively, you can run `cargo install` directly as long as you set the required environment variables and features.
8188

8289
```sh
83-
export CFG_RELEASE=1.45.0-nightly
90+
export CFG_RELEASE=nightly
8491
export CFG_RELEASE_CHANNEL=nightly
8592
cargo install --path . --force --locked --features rustfmt,cargo-fmt
8693
```
8794
(Windows users can use `set` to specify the environment variable values)
8895

89-
This will install `rustfmt` in your `~/.cargo/bin`. Make sure to add `~/.cargo/bin` directory to
96+
This will install `rustfmt` in your `~/.cargo/bin`. Make sure to add the `~/.cargo/bin` directory to
9097
your PATH variable.
9198

92-
## Running
99+
## Usage
100+
101+
Please use `rustfmt --help` to see information about available arguments.
102+
103+
### Running `cargo fmt`
104+
105+
The easiest way to run rustfmt against a project is with `cargo fmt`. `cargo fmt` works on both
106+
single-crate projects and [cargo workspaces](https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html).
107+
Please see `cargo fmt --help` for usage information.
108+
109+
### Running `rustfmt` directly
93110

94-
You can run `rustfmt --help` for information about available arguments.
111+
To format individual files or arbitrary codes from stdin, the `rustfmt` binary should be used. Some
112+
examples follow:
95113

96-
You can run Rustfmt by just typing `rustfmt filename` if you used `cargo
97-
install`. This runs rustfmt on the given file, if the file includes out of line
98-
modules, then we reformat those too. So to run on a whole module or crate, you
99-
just need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also
100-
read data from stdin. Alternatively, you can use `cargo fmt` to format all
101-
binary and library targets of your crate.
114+
- `rustfmt lib.rs main.rs` will format "lib.rs" and "main.rs" in place
115+
- `rustfmt` will read a code from stdin and write formatting to stdout
116+
- `echo "fn main() {}" | rustfmt` would emit "fn main() {}".
117+
118+
For more information, including arguments and emit options, see `rustfmt --help`.
119+
120+
### Verifying code is formatted
102121

103122
When running with `--check`, Rustfmt will exit with `0` if Rustfmt would not
104123
make any formatting changes to the input, and `1` if Rustfmt would make changes.
124+
125+
### Exit codes
126+
105127
In other modes, Rustfmt will exit with `1` if there was some error during
106128
formatting (for example a parsing or internal error) and `0` if formatting
107129
completed without error (whether or not changes were made).
@@ -110,13 +132,12 @@ completed without error (whether or not changes were made).
110132

111133
Rustfmt is designed to be very configurable. You can create a TOML file called
112134
`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/).
135+
directory and it will apply the options in that file. See the [config website](https://rust-lang.github.io/rustfmt/)
136+
for all available options.
116137

117138
By default, Rustfmt uses a style which conforms to the [Rust style guide][style
118139
guide] that has been formalized through the [style RFC
119-
process][fmt rfcs].
140+
process][fmt RFCs].
120141

121142
Configuration options are either stable or unstable. Stable options can always
122143
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).
@@ -125,6 +146,28 @@ Unstable options are not available on stable/beta with Rustfmt v1.x.
125146

126147
See the configuration documentation on the Rustfmt [GitHub page](https://rust-lang.github.io/rustfmt/) for details (look for the `unstable_features` section).
127148

149+
### Differences in rustfmt versions
150+
151+
#### Default formatting of submodules
152+
153+
On an invocation `rustfmt lib.rs`, rustfmt 1.x would format both "lib.rs" and any out-of-file
154+
submodules referenced in "lib.rs", unless the `skip_children` configuration option was true.
155+
156+
With rustfmt 2.x, this behavior requires the `--recursive` flag (#3587). By default, out-of-file
157+
submodules of given files are not formatted.
158+
159+
Note that this only applies to the `rustfmt` binary, and does not impact `cargo fmt`.
160+
161+
#### Construction of config options
162+
163+
Rustfmt 1.x uses only the configuration options declared in the rustfmt configuration file nearest
164+
the directory `rustfmt` is invoked.
165+
166+
Rustfmt 2.x merges configuration options from all configuration files in all parent directories,
167+
with configuration files nearer the current directory having priority.
168+
169+
Please see [Configurations](https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#configuration-file-resolution) for more information and #3881 for the motivating issue.
170+
128171
### Rust's Editions
129172

130173
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
@@ -134,28 +177,13 @@ needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
134177
## Limitations
135178

136179
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.
143-
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
180+
doesn't even need to compile! However, there are some things that
181+
Rustfmt can't do or can't do well. The following list enumerates such limitations:
182+
183+
* A program where any part of the program does not parse (parsing is an early
149184
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.
155185
* Any fragment of a program (i.e., stability guarantees only apply to whole
156186
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).
159187
* Bugs in Rustfmt (like any software, Rustfmt has bugs, we do not consider bug
160188
fixes to break our stability guarantees).
161189

@@ -226,7 +254,7 @@ cargo make test
226254
Or alternatively with `cargo` directly:
227255
```sh
228256
cargo test --all-features
229-
# or
257+
# or
230258
CFG_RELEASE_CHANNEL=nightly CFG_RELEASE=1.45.0-nightly cargo test --all-features
231259
```
232260

@@ -237,13 +265,13 @@ notes above on running rustfmt.
237265

238266
* For things you do not want rustfmt to mangle, use `#[rustfmt::skip]`
239267
* To prevent rustfmt from formatting a macro or an attribute,
240-
use `#[rustfmt::skip::macros(target_macro_name)]` or
268+
use `#[rustfmt::skip::macros(target_macro_name)]` or
241269
`#[rustfmt::skip::attributes(target_attribute_name)]`
242270

243271
Example:
244272

245273
```rust
246-
#![rustfmt::skip::attributes(custom_attribute)]
274+
#![rustfmt::skip::attributes(custom_attribute)]
247275
248276
#[custom_attribute(formatting , here , should , be , Skipped)]
249277
#[rustfmt::skip::macros(html)]

0 commit comments

Comments
 (0)