Skip to content

Commit f65e2e3

Browse files
committed
Backport changes to md
1 parent 19a3364 commit f65e2e3

3 files changed

Lines changed: 56 additions & 78 deletions

File tree

nostarch/appendix.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,11 @@ The `rustfmt` tool reformats your code according to the community code style.
543543
Many collaborative projects use `rustfmt` to prevent arguments about which
544544
style to use when writing Rust: everyone formats their code using the tool.
545545

546-
To install `rustfmt`, enter the following:
547-
548-
```
549-
$ rustup component add rustfmt
550-
```
551-
552-
This command gives you `rustfmt` and `cargo-fmt`, similar to how Rust gives you
553-
both `rustc` and `cargo`. To format any Cargo project, enter the following:
546+
Rust installations include `rustfmt` by default, so you should already have the
547+
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
548+
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
549+
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
550+
any Cargo project, enter the following:
554551

555552
```
556553
$ cargo fmt
@@ -562,9 +559,9 @@ on `rustfmt`, see its documentation at *https://github.com/rust-lang/rustfmt*.
562559

563560
### Fix Your Code with rustfix
564561

565-
The rustfix tool is included with Rust installations and can automatically fix
562+
The `rustfix` tool is included with Rust installations and can automatically fix
566563
compiler warnings that have a clear way to correct the problem that’s likely
567-
what you want. It’s likely you’ve seen compiler warnings before. For example,
564+
what you want. You’ve probably seen compiler warnings before. For example,
568565
consider this code:
569566

570567
Filename: src/main.rs
@@ -576,8 +573,8 @@ fn main() {
576573
}
577574
```
578575

579-
Here, we’re defining variable `x` as mutable, but we never actually mutate it.
580-
Rust warns us about that:
576+
Here, we’re defining the variable `x` as mutable, but we never actually mutate
577+
it. Rust warns us about that:
581578

582579
```
583580
$ cargo build
@@ -615,21 +612,16 @@ fn main() {
615612
}
616613
```
617614

618-
The `x` variable is now immutable, and the warning no longer appears.
615+
The variable `x` is now immutable, and the warning no longer appears.
619616

620617
You can also use the `cargo fix` command to transition your code between
621618
different Rust editions. Editions are covered in Appendix E at *appendix-05-editions.md*.
622619

623620
### More Lints with Clippy
624621

625622
The Clippy tool is a collection of lints to analyze your code so you can catch
626-
common mistakes and improve your Rust code.
627-
628-
To install Clippy, enter the following:
629-
630-
```
631-
$ rustup component add clippy
632-
```
623+
common mistakes and improve your Rust code. Clippy is included with standard
624+
Rust installations.
633625

634626
To run Clippy’s lints on any Cargo project, enter the following:
635627

@@ -640,7 +632,7 @@ $ cargo clippy
640632
For example, say you write a program that uses an approximation of a
641633
mathematical constant, such as pi, as this program does:
642634

643-
Filename: src/main.rs
635+
src/main.rs
644636

645637
```
646638
fn main() {
@@ -650,6 +642,8 @@ fn main() {
650642
}
651643
```
652644

645+
646+
653647
Running `cargo clippy` on this project results in this error:
654648

655649
```
@@ -666,10 +660,11 @@ error: approximate value of `f{32, 64}::consts::PI` found
666660

667661
This error lets you know that Rust already has a more precise `PI` constant
668662
defined, and that your program would be more correct if you used the constant
669-
instead. You would then change your code to use the `PI` constant. The
670-
following code doesn’t result in any errors or warnings from Clippy:
663+
instead. You would then change your code to use the `PI` constant.
671664

672-
Filename: src/main.rs
665+
The following code doesn’t result in any errors or warnings from Clippy:
666+
667+
src/main.rs
673668

674669
```
675670
fn main() {
@@ -679,19 +674,21 @@ fn main() {
679674
}
680675
```
681676

677+
678+
682679
For more information on Clippy, see its documentation at *https://github.com/rust-lang/rust-clippy*.
683680

684681
### IDE Integration Using rust-analyzer
685682

686-
To help IDE integration, the Rust community recommends using
683+
To help with IDE integration, the Rust community recommends using
687684
`rust-analyzer`. This tool is a set of
688-
compiler-centric utilities that speaks the Language Server Protocol, which is a specification for IDEs and programming languages to
685+
compiler-centric utilities that speak Language Server Protocol, which is a specification for IDEs and programming languages to
689686
communicate with each other. Different clients can use `rust-analyzer`, such as
690687
the Rust analyzer plug-in for Visual Studio Code at *https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer*.
691688

692689
Visit the `rust-analyzer` project’s home page
693690
for installation instructions, then install the language server support in your
694-
particular IDE. Your IDE will gain abilities such as autocompletion, jump to
691+
particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
695692
definition, and inline errors.
696693

697694
## Appendix E - Editions

nostarch/appendix_d.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ style to use when writing Rust: everyone formats their code using the tool.
2020

2121
Rust installations include `rustfmt` by default, so you should already have the
2222
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
23-
analagous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
23+
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
2424
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
2525
any Cargo project, enter the following:
2626

@@ -42,34 +42,30 @@ example, consider this code:
4242
Filename: src/main.rs
4343

4444
```
45-
fn do_something() {}
46-
4745
fn main() {
48-
for i in 0..100 {
49-
do_something();
50-
}
46+
let mut x = 42;
47+
println!("{x}");
5148
}
5249
```
5350

54-
Here, we’re calling the `do_something` function 100 times, but we never use the
55-
variable `i` in the body of the `for` loop. Rust warns us about that:
51+
Here, we’re defining the variable `x` as mutable, but we never actually mutate
52+
it. Rust warns us about that:
5653

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

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

@@ -86,16 +82,13 @@ code:
8682
Filename: src/main.rs
8783

8884
```
89-
fn do_something() {}
90-
9185
fn main() {
92-
for _i in 0..100 {
93-
do_something();
94-
}
86+
let x = 42;
87+
println!("{x}");
9588
}
9689
```
9790

98-
The `for` loop variable is now named `_i`, and the warning no longer appears.
91+
The variable `x` is now immutable, and the warning no longer appears.
9992

10093
You can also use the `cargo fix` command to transition your code between
10194
different Rust editions. Editions are covered in Appendix E.
@@ -157,7 +150,7 @@ fn main() {
157150
```
158151

159152
For more information on Clippy, see its documentation at
160-
*https://github.com/rust-lang/rust-clippy**.*
153+
*https://github.com/rust-lang/rust-clippy*.
161154

162155
## IDE Integration Using rust-analyzer
163156

@@ -171,5 +164,5 @@ languages to communicate with each other. Different clients can use
171164
Visit the `rust-analyzer` project’s home page at
172165
*https://rust-analyzer.github.io* for installation instructions, then install
173166
the language server support in your particular IDE. Your IDE will gain
174-
capabilities such as autocompletion, jump to definition, and inline errors
167+
capabilities such as autocompletion, jump to definition, and inline errors.
175168

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,12 @@ The `rustfmt` tool reformats your code according to the community code style.
1010
Many collaborative projects use `rustfmt` to prevent arguments about which
1111
style to use when writing Rust: everyone formats their code using the tool.
1212

13-
Rust installations include rustfmt by default, so you should already have the
13+
Rust installations include `rustfmt` by default, so you should already have the
1414
programs `rustfmt` and `cargo-fmt` on your system. These two commands are
1515
analogous to `rustc` and `cargo` in that `rustfmt` allows finer-grained control
1616
and `cargo-fmt` understands conventions of a project that uses Cargo. To format
1717
any Cargo project, enter the following:
1818

19-
```sh
20-
$ cargo fmt
21-
```
22-
23-
Running this command reformats all the Rust code in the current crate. This
24-
should only change the code style, not the code semantics.
25-
26-
This command gives you `rustfmt` and `cargo-fmt`, similar to how Rust gives you
27-
both `rustc` and `cargo`. To format any Cargo project, enter the following:
28-
2919
```console
3020
$ cargo fmt
3121
```
@@ -34,13 +24,11 @@ Running this command reformats all the Rust code in the current crate. This
3424
should only change the code style, not the code semantics. For more information
3525
on `rustfmt`, see [its documentation][rustfmt].
3626

37-
[rustfmt]: https://github.com/rust-lang/rustfmt
38-
3927
### Fix Your Code with `rustfix`
4028

4129
The `rustfix` tool is included with Rust installations and can automatically fix
4230
compiler warnings that have a clear way to correct the problem that’s likely
43-
what you want. It’s likely you’ve seen compiler warnings before. For example,
31+
what you want. You’ve probably seen compiler warnings before. For example,
4432
consider this code:
4533

4634
<span class="filename">Filename: src/main.rs</span>
@@ -92,7 +80,7 @@ fn main() {
9280
}
9381
```
9482

95-
The `x` variable is now immutable, and the warning no longer appears.
83+
The variable `x` is now immutable, and the warning no longer appears.
9684

9785
You can also use the `cargo fix` command to transition your code between
9886
different Rust editions. Editions are covered in [Appendix E][editions].
@@ -140,8 +128,9 @@ error: approximate value of `f{32, 64}::consts::PI` found
140128

141129
This error lets you know that Rust already has a more precise `PI` constant
142130
defined, and that your program would be more correct if you used the constant
143-
instead. You would then change your code to use the `PI` constant. The
144-
following code doesn’t result in any errors or warnings from Clippy:
131+
instead. You would then change your code to use the `PI` constant.
132+
133+
The following code doesn’t result in any errors or warnings from Clippy:
145134

146135
<Listing file-name="src/main.rs">
147136

@@ -157,24 +146,23 @@ fn main() {
157146

158147
For more information on Clippy, see [its documentation][clippy].
159148

160-
[clippy]: https://github.com/rust-lang/rust-clippy
161-
162149
### IDE Integration Using `rust-analyzer`
163150

164-
To help IDE integration, the Rust community recommends using
151+
To help with IDE integration, the Rust community recommends using
165152
[`rust-analyzer`][rust-analyzer]<!-- ignore -->. This tool is a set of
166-
compiler-centric utilities that speaks the [Language Server Protocol][lsp]<!--
153+
compiler-centric utilities that speak [Language Server Protocol][lsp]<!--
167154
ignore -->, which is a specification for IDEs and programming languages to
168155
communicate with each other. Different clients can use `rust-analyzer`, such as
169156
[the Rust analyzer plug-in for Visual Studio Code][vscode].
170157

171-
[lsp]: http://langserver.org/
172-
[vscode]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
173-
174158
Visit the `rust-analyzer` project’s [home page][rust-analyzer]<!-- ignore -->
175159
for installation instructions, then install the language server support in your
176-
particular IDE. Your IDE will gain abilities such as autocompletion, jump to
160+
particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
177161
definition, and inline errors.
178162

179-
[rust-analyzer]: https://rust-analyzer.github.io
163+
[rustfmt]: https://github.com/rust-lang/rustfmt
180164
[editions]: appendix-05-editions.md
165+
[clippy]: https://github.com/rust-lang/rust-clippy
166+
[rust-analyzer]: https://rust-analyzer.github.io
167+
[lsp]: http://langserver.org/
168+
[vscode]: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer

0 commit comments

Comments
 (0)