Table B-10 shows the contexts in which square brackets are used.
@ -542,14 +543,11 @@ The `rustfmt` tool reformats your code according to the community code style.
@@ -542,14 +543,11 @@ 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:
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
```
$ cargo fmt
@ -561,9 +559,9 @@ on `rustfmt`, see its documentation at *https://github.com/rust-lang/rustfmt*.
@@ -561,9 +559,9 @@ on `rustfmt`, see its documentation at *https://github.com/rust-lang/rustfmt*.
### Fix Your Code with rustfix
The rustfix tool is included with Rust installations and can automatically fix
The `rustfix` tool is included with Rust installations and can automatically fix
compiler warnings that have a clear way to correct the problem that’s likely
what you want. It’s likely you’ve seen compiler warnings before. For example,
what you want. You’ve probably seen compiler warnings before. For example,
The `x` variable is now immutable, and the warning no longer appears.
The variable `x` is now immutable, 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 at *appendix-05-editions.md*.
@ -622,13 +620,8 @@ different Rust editions. Editions are covered in Appendix E at *appendix-05-edit
@@ -622,13 +620,8 @@ different Rust editions. Editions are covered in Appendix E at *appendix-05-edit
### More Lints with Clippy
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
```
common mistakes and improve your Rust code. Clippy is included with standard
Rust installations.
To run Clippy’s lints on any Cargo project, enter the following:
Running `cargo clippy` on this project results in this error:
```
@ -665,10 +660,11 @@ error: approximate value of `f{32, 64}::consts::PI` found
@@ -665,10 +660,11 @@ error: approximate value of `f{32, 64}::consts::PI` found
This error lets you know that Rust already has a more precise `PI` constant
defined, 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:
instead. You would then change your code to use the `PI` constant.
Filename: src/main.rs
The following code doesn’t result in any errors or warnings from Clippy:
@ -20,7 +20,7 @@ style to use when writing Rust: everyone formats their code using the tool.
@@ -20,7 +20,7 @@ style to use when writing Rust: everyone formats their code using the tool.
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analagous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
@ -42,34 +42,30 @@ example, consider this code:
@@ -42,34 +42,30 @@ example, consider this code:
Filename: src/main.rs
```
fn do_something() {}
fn main() {
for i in 0..100 {
do_something();
}
let mut x = 42;
println!("{x}");
}
```
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:
Here, we’re defining the variable `x` as mutable, but we never actually mutate
For more information on Clippy, see its documentation at
*https://github.com/rust-lang/rust-clippy**.*
*https://github.com/rust-lang/rust-clippy*.
## IDE Integration Using rust-analyzer
@ -171,5 +164,5 @@ languages to communicate with each other. Different clients can use
@@ -171,5 +164,5 @@ languages to communicate with each other. Different clients can use
Visit the `rust-analyzer` project’s home page at
*https://rust-analyzer.github.io* for installation instructions, then install
the language server support in your particular IDE. Your IDE will gain
capabilities such as autocompletion, jump to definition, and inline errors
capabilities such as autocompletion, jump to definition, and inline errors.
@ -191,7 +191,7 @@ Table B-9 shows the contexts in which curly braces are used.
@@ -191,7 +191,7 @@ Table B-9 shows the contexts in which curly braces are used.
| Context | Explanation |
| ------------ | ---------------- |
| `{...}` | Block expression |
| `Type {...}` | `struct` literal |
| `Type {...}` | Struct literal |
Table B-10 shows the contexts in which square brackets are used.
@ -10,22 +10,12 @@ The `rustfmt` tool reformats your code according to the community code style.
@@ -10,22 +10,12 @@ 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.
Rust installations include rustfmt by default, so you should already have the
Rust installations include `rustfmt` by default, so you should already have the
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
any Cargo project, enter the following:
```sh
$ 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.
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:
```console
$ cargo fmt
```
@ -34,13 +24,11 @@ Running this command reformats all the Rust code in the current crate. This
@@ -34,13 +24,11 @@ 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 `rustfmt`, see [its documentation][rustfmt].
[rustfmt]: https://github.com/rust-lang/rustfmt
### Fix Your Code with `rustfix`
The `rustfix` tool is included with Rust installations and can automatically fix
compiler warnings that have a clear way to correct the problem that’s likely
what you want. It’s likely you’ve seen compiler warnings before. For example,
what you want. You’ve probably seen compiler warnings before. For example,
The `x` variable is now immutable, and the warning no longer appears.
The variable `x` is now immutable, 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][editions].
@ -140,8 +128,9 @@ error: approximate value of `f{32, 64}::consts::PI` found
@@ -140,8 +128,9 @@ error: approximate value of `f{32, 64}::consts::PI` found
This error lets you know that Rust already has a more precise `PI` constant
defined, 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:
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: