The rustfmt
tool reformats your code according to the community code style. Many collaborative projects use rustfmt
to prevent arguments about which style to use when writing Rust: everyone formats their code using the tool.
To install rustfmt
, enter the following:
$ rustup component add rustfmt
This command gives you rustfmt
and cargo-fmt
, similar to how Rust gives you both rustc
and cargo
. To format any Cargo project, enter the following:
$ cargo fmt
Running this command reformats all the Rust code in the current crate. This should only change the code style, not the code semantics. For more information on , see .
The rustfix tool is included with Rust installations and can automatically fix some compiler warnings. If you’ve written code in Rust, you’ve probably seen compiler warnings. For example, consider this code:
Filename: src/main.rs
Here, we’re calling the do_something
function 100 times, but we never use the variable i
in the body of the for
loop. Rust warns us about that:
$ cargo build Compiling myprogram v0.1.0 (file:///projects/myprogram) warning: unused variable: `i` --> src/main.rs:4:9 | 4 | for i in 1..100 { | ^ help: consider using `_i` instead | = note: #[warn(unused_variables)] on by default Finished dev [unoptimized + debuginfo] target(s) in 0.50s
$ cargo fix Checking myprogram v0.1.0 (file:///projects/myprogram) Fixing src/main.rs (1 fix) Finished dev [unoptimized + debuginfo] target(s) in 0.59s
When we look at src/main.rs again, we’ll see that cargo fix
has changed the code:
Filename: src/main.rs
The for
loop variable is now named , and the warning no longer appears.
You can also use the cargo fix
command to transition your code between different Rust editions. Editions are covered in Appendix E.
The Clippy tool is a collection of lints to analyze your code so you can catch common mistakes and improve your Rust code.
To install Clippy, enter the following:
$ rustup component add clippy
To run Clippy’s lints on any Cargo project, enter the following:
$ cargo clippy
Filename: src/main.rs
Running cargo clippy
on this project results in this error:
error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly --> src/main.rs:2:13 | 2 | let x = 3.1415; | ^^^^^^ | = note: #[deny(clippy::approx_constant)] on by default = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#approx_constant
This error lets you know that Rust has this constant defined more precisely and that your program would be more correct if you used the constant instead. You would then change your code to use the PI
constant. The following code doesn’t result in any errors or warnings from Clippy:
Filename: src/main.rs
For more information on Clippy, see its documentation.
To help IDE integration, the Rust project distributes the Rust Language Server (rls
). This tool speaks the , which is a specification for IDEs and programming languages to communicate with each other. Different clients can use the rls
, such as the Rust plug-in for Visual Studio Code.
To install the rls
, enter the following:
$ rustup component add rls
Then install the language server support in your particular IDE; you’ll gain abilities such as autocompletion, jump to definition, and inline errors.