Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example for cargo fix #4226

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/appendix-04-useful-development-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,30 @@ consider this code:
<span class="filename">Filename: src/main.rs</span>

```rust
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 variable `x` as mutable, but we never actually mutate it.
Rust warns us about that:

```console
$ cargo build
Compiling myprogram v0.1.0 (file:///projects/myprogram)
warning: unused variable: `i`
--> src/main.rs:4:9
warning: variable does not need to be mutable
--> src/main.rs:2:9
|
4 | for i in 0..100 {
| ^ help: consider using `_i` instead
2 | let mut x = 0;
| ----^
| |
| help: remove this `mut`
|
= note: #[warn(unused_variables)] on by default

Finished dev [unoptimized + debuginfo] target(s) in 0.50s
= note: `#[warn(unused_mut)]` on by default
```

The warning suggests that we use `_i` as a name instead: the underscore
indicates that we intend for this variable to be unused. We can automatically
The warning suggests that we remove the `mut` keyword. We can automatically
apply that suggestion using the `rustfix` tool by running the command `cargo
fix`:

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

```rust
fn do_something() {}

fn main() {
for _i in 0..100 {
do_something();
}
let x = 42;
println!("{x}");
}
```

The `for` loop variable is now named `_i`, and the warning no longer appears.
The `x` variable 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].
Expand Down