]> git.lizzy.rs Git - rust.git/blob - README.md
Information for new contributors
[rust.git] / README.md
1 # 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].
7
8
9 ## Installation
10
11 > **Note:** this method currently requires you to be running a nightly install
12 > of Rust as `cargo install` has not yet made its way onto the stable channel.
13
14 ```
15 cargo install --git https://github.com/nrc/rustfmt
16 ```
17
18 or if you're using [`multirust`](https://github.com/brson/multirust)
19
20 ```
21 multirust run nightly cargo install --git https://github.com/nrc/rustfmt
22 ```
23
24
25 ## Running Rustfmt from Vim
26
27 See [instructions](http://johannh.me/blog/rustfmt-vim.html).
28
29
30 ## How to build and test
31
32 First make sure you've got Rust **1.4.0** or greater available, then:
33
34 `cargo build` to build.
35
36 `cargo test` to run all tests.
37
38 `cargo run -- filename` to run on a file, if the file includes out of line
39 modules, then we reformat those too. So to run on a whole module or crate, you
40 just need to run on the top file.
41
42 You'll probably want to specify the write mode. Currently, there are the
43 replace, overwrite, display and coverage modes. The replace mode is the default
44 and overwrites the original files after renaming them. In overwrite mode,
45 rustfmt does not backup the source files. To print the output to stdout, use the
46 display mode. The write mode can be set by passing the `--write-mode` flag on
47 the command line.
48
49 `cargo run -- filename --write-mode=display` prints the output of rustfmt to the
50 screen, for example.
51
52
53 ## What style does Rustfmt use?
54
55 Rustfmt is designed to be very configurable. You can create a TOML file called
56 rustfmt.toml, place it in the project directory and it will apply the options
57 in that file. See `cargo run --help-config` for the options which are available,
58 or if you prefer to see source code, [src/config.rs].
59
60 By default, Rustfmt uses a style which (mostly) confirms to the
61 [Rust style guidelines](https://github.com/rust-lang/rust/tree/master/src/doc/style).
62 There are many details which the style guidelines do not cover, and in these
63 cases we try to adhere to a style similar to that used in the
64 [Rust repo](https://github.com/rust-lang/rust). Once Rustfmt is more complete, and
65 able to re-format large repositories like Rust, we intend to go through the Rust
66 RFC process to nail down the default style in detail.
67
68 If there are styling choices you don't agree with, we are usually happy to add
69 options covering different styles. File an issue, or even better, submit a PR.
70
71
72 ## Gotchas
73
74 * For things you do not want rustfmt to mangle, use one of
75     ```rust
76     #[rustfmt_skip]
77     #[cfg_attr(rustfmt, rustfmt_skip)]
78     ```
79 * When you run rustfmt, place a file named rustfmt.toml in target file
80   directory or its parents to override the default settings of rustfmt.
81 * After successful compilation, a `rustfmt` executable can be found in the
82   target directory.