Skip to content

Commit d4d2c18

Browse files
authored
Merge pull request #4226 from GoldsteinE/fix/rustfix-example
Fix example for `cargo fix`
2 parents e2fa431 + 8698ba5 commit d4d2c18

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/appendix-04-useful-development-tools.md

+15-22
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,30 @@ consider this code:
3939
<span class="filename">Filename: src/main.rs</span>
4040

4141
```rust
42-
fn do_something() {}
43-
4442
fn main() {
45-
for i in 0..100 {
46-
do_something();
47-
}
43+
let mut x = 42;
44+
println!("{x}");
4845
}
4946
```
5047

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:
48+
Here, we’re defining variable `x` as mutable, but we never actually mutate it.
49+
Rust warns us about that:
5350

5451
```console
5552
$ cargo build
5653
Compiling myprogram v0.1.0 (file:///projects/myprogram)
57-
warning: unused variable: `i`
58-
--> src/main.rs:4:9
54+
warning: variable does not need to be mutable
55+
--> src/main.rs:2:9
5956
|
60-
4 | for i in 0..100 {
61-
| ^ help: consider using `_i` instead
57+
2 | let mut x = 0;
58+
| ----^
59+
| |
60+
| help: remove this `mut`
6261
|
63-
= note: #[warn(unused_variables)] on by default
64-
65-
Finished dev [unoptimized + debuginfo] target(s) in 0.50s
62+
= note: `#[warn(unused_mut)]` on by default
6663
```
6764

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
65+
The warning suggests that we remove the `mut` keyword. We can automatically
7066
apply that suggestion using the `rustfix` tool by running the command `cargo
7167
fix`:
7268

@@ -83,16 +79,13 @@ code:
8379
<span class="filename">Filename: src/main.rs</span>
8480

8581
```rust
86-
fn do_something() {}
87-
8882
fn main() {
89-
for _i in 0..100 {
90-
do_something();
91-
}
83+
let x = 42;
84+
println!("{x}");
9285
}
9386
```
9487

95-
The `for` loop variable is now named `_i`, and the warning no longer appears.
88+
The `x` variable is now immutable, and the warning no longer appears.
9689

9790
You can also use the `cargo fix` command to transition your code between
9891
different Rust editions. Editions are covered in [Appendix E][editions].

0 commit comments

Comments
 (0)