1 ## Appendix D - Useful Development Tools
3 In this appendix, we talk about some useful development tools that the Rust
4 project provides. We’ll look at automatic formatting, quick ways to apply
5 warning fixes, a linter, and integrating with IDEs.
7 ### Automatic Formatting with `rustfmt`
9 The `rustfmt` tool reformats your code according to the community code style.
10 Many collaborative projects use `rustfmt` to prevent arguments about which
11 style to use when writing Rust: everyone formats their code using the tool.
13 To install `rustfmt`, enter the following:
16 $ rustup component add rustfmt
19 This command gives you `rustfmt` and `cargo-fmt`, similar to how Rust gives you
20 both `rustc` and `cargo`. To format any Cargo project, enter the following:
26 Running this command reformats all the Rust code in the current crate. This
27 should only change the code style, not the code semantics. For more information
28 on `rustfmt`, see [its documentation][rustfmt].
30 [rustfmt]: https://github.com/rust-lang/rustfmt
32 ### Fix Your Code with `rustfix`
34 The rustfix tool is included with Rust installations and can automatically fix
35 compiler warnings that have a clear way to correct the problem that’s likely
36 what you want. It’s likely you’ve seen compiler warnings before. For example,
39 <span class="filename">Filename: src/main.rs</span>
51 Here, we’re calling the `do_something` function 100 times, but we never use the
52 variable `i` in the body of the `for` loop. Rust warns us about that:
56 Compiling myprogram v0.1.0 (file:///projects/myprogram)
57 warning: unused variable: `i`
61 | ^ help: consider using `_i` instead
63 = note: #[warn(unused_variables)] on by default
65 Finished dev [unoptimized + debuginfo] target(s) in 0.50s
68 The warning suggests that we use `_i` as a name instead: the underscore
69 indicates that we intend for this variable to be unused. We can automatically
70 apply that suggestion using the `rustfix` tool by running the command `cargo
75 Checking myprogram v0.1.0 (file:///projects/myprogram)
76 Fixing src/main.rs (1 fix)
77 Finished dev [unoptimized + debuginfo] target(s) in 0.59s
80 When we look at *src/main.rs* again, we’ll see that `cargo fix` has changed the
83 <span class="filename">Filename: src/main.rs</span>
95 The `for` loop variable is now named `_i`, and the warning no longer appears.
97 You can also use the `cargo fix` command to transition your code between
98 different Rust editions. Editions are covered in Appendix E.
100 ### More Lints with Clippy
102 The Clippy tool is a collection of lints to analyze your code so you can catch
103 common mistakes and improve your Rust code.
105 To install Clippy, enter the following:
108 $ rustup component add clippy
111 To run Clippy’s lints on any Cargo project, enter the following:
117 For example, say you write a program that uses an approximation of a
118 mathematical constant, such as pi, as this program does:
120 <span class="filename">Filename: src/main.rs</span>
126 println!("the area of the circle is {}", x * r * r);
130 Running `cargo clippy` on this project results in this error:
133 error: approximate value of `f{32, 64}::consts::PI` found
139 = note: `#[deny(clippy::approx_constant)]` on by default
140 = help: consider using the constant directly
141 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
144 This error lets you know that Rust already has a more precise `PI` constant
145 defined, and that your program would be more correct if you used the constant
146 instead. You would then change your code to use the `PI` constant. The
147 following code doesn’t result in any errors or warnings from Clippy:
149 <span class="filename">Filename: src/main.rs</span>
153 let x = std::f64::consts::PI;
155 println!("the area of the circle is {}", x * r * r);
159 For more information on Clippy, see [its documentation][clippy].
161 [clippy]: https://github.com/rust-lang/rust-clippy
163 ### IDE Integration Using `rust-analyzer`
165 To help IDE integration, the Rust community recommends using
166 [`rust-analyzer`][rust-analyzer]<!-- ignore -->. This tool is a set of
167 compiler-centric utilities that speaks the [Language Server Protocol][lsp]<!--
168 ignore -->, which is a specification for IDEs and programming languages to
169 communicate with each other. Different clients can use `rust-analyzer`, such as
170 [the Rust analyzer plug-in for Visual Studio Code][vscode].
172 [lsp]: http://langserver.org/
173 [vscode]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
175 Visit the `rust-analyzer` project’s [home page][rust-analyzer]<!-- ignore -->
176 for installation instructions, then install the language server support in your
177 particular IDE. Your IDE will gain abilities such as autocompletion, jump to
178 definition, and inline errors.
180 [rust-analyzer]: https://rust-analyzer.github.io