]> git.lizzy.rs Git - rust.git/blob - README.md
Merge pull request #1719 from topecongiro/macro-on-rhs
[rust.git] / README.md
1 # rustfmt [![Build Status](https://travis-ci.org/rust-lang-nursery/rustfmt.svg)](https://travis-ci.org/rust-lang-nursery/rustfmt) [![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-lang-nursery/rustfmt?svg=true)](https://ci.appveyor.com/api/projects/status/github/rust-lang-nursery/rustfmt)
2
3 A tool for formatting Rust code according to style guidelines.
4
5 If you'd like to help out (and you should, it's a fun project!), see
6 [Contributing.md](Contributing.md).
7
8 We are changing the default style used by rustfmt. There is an ongoing [RFC process](https://github.com/rust-lang-nursery/fmt-rfcs).
9 The last version using the old style was 0.8.6. From 0.9 onwards, the RFC style
10 is the default. If you want the old style back, you can use [legacy-rustfmt.toml](legacy-rustfmt.toml)
11 as your rustfmt.toml.
12
13 The current `master` branch uses libsyntax (part of the compiler). It is
14 published as `rustfmt-nightly`. The `syntex` branch uses Syntex instead of
15 libsyntax, it is published (for now) as `rustfmt`. Most development happens on
16 the `master` branch, however, this only supports nightly toolchains. If you use
17 stable or beta Rust toolchains, you must use the Syntex version (which is likely
18 to be a bit out of date). Version 0.1 of rustfmt-nightly is forked from version
19 0.9 of the syntex branch.
20
21
22 ## Quick start
23
24 You must be using a nightly compiler toolchain.
25
26 To install:
27
28 ```
29 cargo install rustfmt-nightly
30 ```
31
32 to run on a cargo project in the current working directory:
33
34 ```
35 cargo fmt
36 ```
37
38 ## Installation
39
40 ```
41 cargo install rustfmt-nightly
42 ```
43
44 or if you're using [Rustup](https://www.rustup.rs/)
45
46 ```
47 rustup run nightly cargo install rustfmt-nightly
48 ```
49
50 If you don't have a nightly toolchain, you can add it using rustup:
51
52 ```
53 rustup install nightly
54 ```
55
56 You can make the nightly toolchain the default by running:
57
58 ```
59 rustup default nightly
60 ```
61
62 If you choose not to do that you'll have to run rustfmt using `rustup run ...`
63 or by adding `+nightly` to the cargo invocation.
64
65 Usually cargo-fmt, which enables usage of Cargo subcommand `cargo fmt`, is
66 installed alongside rustfmt. To only install rustfmt run
67
68 ```
69 cargo install --no-default-features rustfmt-nightly
70 ```
71 ## Installing from source
72
73 To install from source, first checkout to the tag or branch you want to install, then issue
74 ```
75 cargo install --path  .
76 ```
77
78 This will install `rustfmt` in your `~/.cargo/bin`. Make sure to add `~/.cargo/bin` directory to
79 your PATH variable.
80
81
82 ## Running
83
84 You can run Rustfmt by just typing `rustfmt filename` if you used `cargo
85 install`. This runs rustfmt on the given file, if the file includes out of line
86 modules, then we reformat those too. So to run on a whole module or crate, you
87 just need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also
88 read data from stdin. Alternatively, you can use `cargo fmt` to format all
89 binary and library targets of your crate.
90
91 You'll probably want to specify the write mode. Currently, there are modes for
92 diff, replace, overwrite, display, coverage, and checkstyle.
93
94 * `replace` Is the default and overwrites the original files after creating backups of the files.
95 * `overwrite` Overwrites the original files _without_ creating backups.
96 * `display` Will print the formatted files to stdout.
97 * `diff` Will print a diff between the original files and formatted files to stdout.
98          Will also exit with an error code if there are any differences.
99 * `checkstyle` Will output the lines that need to be corrected as a checkstyle XML file,
100   that can be used by tools like Jenkins.
101
102 The write mode can be set by passing the `--write-mode` flag on
103 the command line. For example `rustfmt --write-mode=display src/filename.rs`
104
105 `cargo fmt` uses `--write-mode=replace` by default.
106
107 If you want to restrict reformatting to specific sets of lines, you can
108 use the `--file-lines` option. Its argument is a JSON array of objects
109 with `file` and `range` properties, where `file` is a file name, and
110 `range` is an array representing a range of lines like `[7,13]`. Ranges
111 are 1-based and inclusive of both end points. Specifying an empty array
112 will result in no files being formatted. For example,
113
114 ```
115 rustfmt --file-lines '[
116     {"file":"src/lib.rs","range":[7,13]},
117     {"file":"src/lib.rs","range":[21,29]},
118     {"file":"src/foo.rs","range":[10,11]},
119     {"file":"src/foo.rs","range":[15,15]}]'
120 ```
121
122 would format lines `7-13` and `21-29` of `src/lib.rs`, and lines `10-11`,
123 and `15` of `src/foo.rs`. No other files would be formatted, even if they
124 are included as out of line modules from `src/lib.rs`.
125
126 If `rustfmt` successfully reformatted the code it will exit with `0` exit
127 status. Exit status `1` signals some unexpected error, like an unknown option or
128 a failure to read a file. Exit status `2` is returned if there are syntax errors
129 in the input files. `rustfmt` can't format syntatically invalid code. Finally,
130 exit status `3` is returned if there are some issues which can't be resolved
131 automatically. For example, if you have a very long comment line `rustfmt`
132 doesn't split it. Instead it prints a warning and exits with `3`.
133
134 You can run `rustfmt --help` for more information.
135
136
137 ## Running Rustfmt from your editor
138
139 * [Vim](https://github.com/rust-lang/rust.vim#formatting-with-rustfmt)
140 * [Emacs](https://github.com/rust-lang/rust-mode)
141 * [Sublime Text 3](https://packagecontrol.io/packages/RustFmt)
142 * [Atom](atom.md)
143 * 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.
144
145 ## Checking style on a CI server
146
147 To keep your code base consistently formatted, it can be helpful to fail the CI build
148 when a pull request contains unformatted code. Using `--write-mode=diff` instructs
149 rustfmt to exit with an error code if the input is not formatted correctly.
150 It will also print any found differences.
151
152 (These instructions use the Syntex version of Rustfmt. If you want to use the
153 nightly version replace `install rustfmt` with `install rustfmt-nightly`,
154 however you must then only run this with the nightly toolchain).
155
156 A minimal Travis setup could look like this:
157
158 ```yaml
159 language: rust
160 cache: cargo
161 before_script:
162 - export PATH="$PATH:$HOME/.cargo/bin"
163 - which rustfmt || cargo install rustfmt
164 script:
165 - cargo fmt -- --write-mode=diff
166 - cargo build
167 - cargo test
168 ```
169
170 Note that using `cache: cargo` is optional but highly recommended to speed up the installation.
171
172 ## How to build and test
173
174 `cargo build` to build.
175
176 `cargo test` to run all tests.
177
178 To run rustfmt after this, use `cargo run --bin rustfmt -- filename`. See the
179 notes above on running rustfmt.
180
181
182 ## Configuring Rustfmt
183
184 Rustfmt is designed to be very configurable. You can create a TOML file called
185 `rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent
186 directory and it will apply the options in that file. See `rustfmt
187 --config-help` for the options which are available, or if you prefer to see
188 visual style previews, [Configurations.md](Configurations.md).
189
190 By default, Rustfmt uses a style which (mostly) conforms to the
191 [Rust style guidelines](https://doc.rust-lang.org/1.12.0/style/README.html).
192 There are many details which the style guidelines do not cover, and in these
193 cases we try to adhere to a style similar to that used in the
194 [Rust repo](https://github.com/rust-lang/rust). Once Rustfmt is more complete, and
195 able to re-format large repositories like Rust, we intend to go through the Rust
196 RFC process to nail down the default style in detail.
197
198 If there are styling choices you don't agree with, we are usually happy to add
199 options covering different styles. File an issue, or even better, submit a PR.
200
201
202 ## Tips
203
204 * For things you do not want rustfmt to mangle, use one of
205
206     ```rust
207     #[rustfmt_skip]  // requires nightly and #![feature(custom_attribute)] in crate root
208     #[cfg_attr(rustfmt, rustfmt_skip)]  // works in stable
209     ```
210 * When you run rustfmt, place a file named `rustfmt.toml` or `.rustfmt.toml` in
211   target file directory or its parents to override the default settings of
212   rustfmt.
213 * After successful compilation, a `rustfmt` executable can be found in the
214   target directory.
215 * If you're having issues compiling Rustfmt (or compile errors when trying to
216   install), make sure you have the most recent version of Rust installed.
217
218
219 ## License
220
221 Rustfmt is distributed under the terms of both the MIT license and the
222 Apache License (Version 2.0).
223
224 See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details.