Updated 'src/appendix/dev_tools.md'.

This commit is contained in:
Hector PENG
2026-05-01 05:28:08 +08:00
parent 2e3fea98af
commit 613353dd59
5 changed files with 71 additions and 73 deletions

View File

@@ -1,5 +1,7 @@
use std::f64::consts;
fn main() { fn main() {
let x = std::f64::consts::PI; let x = consts::PI;
let r = 8.0; let r = 8.0;
println!("圆的面积为 {}", x * r * r); println!("圆的面积为 {}", x * r * r);
} }

View File

@@ -0,0 +1,6 @@
[package]
name = "myprogram"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -0,0 +1,4 @@
fn main() {
let x = 42;
println!("{x}");
}

View File

@@ -140,7 +140,7 @@
- [A - 关键字](appendix/keywords.md) - [A - 关键字](appendix/keywords.md)
- [B - 运算符与符号](appendix/ops_and_symbols.md) - [B - 运算符与符号](appendix/ops_and_symbols.md)
- [C - 派生特质](appendix/derivable_traits.md) - [C - 派生特质](appendix/derivable_traits.md)
- [D - 有用开发工具](appendix/dev_tools.md) - [D - 有用开发工具](appendix/dev_tools.md)
- [E - 关于版本](appendix/editions.md) - [E - 关于版本](appendix/editions.md)
- [F - 本书译本](appendix/translations.md) - [F - 本书译本](appendix/translations.md)
- [G - Rust 是怎样构造出来的与 “每日 Rust”](appendix/releases.md) - [G - Rust 是怎样构造出来的与 “每日 Rust”](appendix/releases.md)

View File

@@ -1,113 +1,98 @@
# 附录 D一些有用开发工具 # 附录 D有用开发工具
附录中,咱们会讲到 Rust 项目提供的一些有用的开发工具。们将看看自动格式化、应用警告修复的一些快速方法、一种代码静态分析工具,a linter以及与多种 IDE 的集成。 这个附录中,我们讨论 Rust 项目提供的一些有用的开发工具。们将探讨自动格式化、快速修复告警的方法、代码静态分析工具,以及与 IDE 的集成。
## 使用 `rustfmt` 自动格式化 ## 通过 `rustfmt` 自动格式化
**Automatic Formatting with `rustfmt`** `rustfmt` 工具会依据社区编码风格,重新格式化咱们的代码。许多协作项目都使用 `rustfmt`,以防止在编写 Rust 时因风格选择而产生争议:每个人都使用这个工具来格式化自己的代码。
Rust 的安装默认包含 `rustfmt`,因此咱们的系统上应该已经安装了 `rustfmt``cargo-fmt` 这两个程序。这两个命令与 `rustc``cargo` 类似:`rustfmt` 允许更细粒度的控制,而 `cargo-fmt` 则能理解使用 Cargo 项目的约定。要格式化任何 Cargo 项目,请输入以下命令:
`rustfmt` 工具会依据社区编码风格,重新格式化咱们的代码。许多协作项目,都使用了 `rustfmt` 来防止有关编写 Rust 时使用何种风格方面的争论:每个人都使用这个工具来格式化他们的代码。
要安装 `rustfmt`,请键入下面的命令:
```console
$ rustup component add rustfmt
```
如同 Rust 会同时给到 `rustc``cargo` 一样,此命令会给到咱们 `rustfmt``cargo-fmt`。要格式化任何 Cargo 项目,请敲入下面的命令:
```console ```console
$ cargo fmt $ cargo fmt
``` ```
运行此命令,会重新格式化当前代码箱中全部的 Rust 代码。这只改变编码风格,而不会改变代码语义。关 `rustfmt` 的更多信息,请参阅 [其文档](https://github.com/rust-lang/rustfmt). 运行这个命令将重新格式化当前代码箱中的所有 Rust 代码。这只改变代码的格式,不会改变代码语义。`rustfmt` 的更多信息,请参阅 [其文档](https://github.com/rust-lang/rustfmt).
## 使用 `rustfix` 修复咱们的代码 ## 通过 `rustfix` 修复代码
**Fix Your Code with `rustfix`** `rustfix` 工具包含在 Rust 安装中,能够修复那些有明确修正问题的方法的一些编译器告警,大致是咱们希望的。咱们可能之前就已见到过编译器告警。例如,设想下面这段代码:
`rustfix` 工具已被 Rust 安装所包含,并可大致以咱们想要的方式,修复那些有着明确纠正问题方法的一些编译器告警。咱们之前大概率已经见到过编译器告警了。比如,设想有下面这段代码:
文件名:`src/main.rs` 文件名:`src/main.rs`
```rust ```rust
fn do_something() {}
fn main() { fn main() {
for i in 0..100 { let mut x = 42;
do_something(); println!("{x}");
}
} }
``` ```
此处,咱们正调用 `do_something` 函数 100 次,但咱们在 `for` 循环的代码体中,从未用到那个变量 `i`。Rust 会就此对咱们发出告警: 在这里,我们定义变量 `x` 为可变的,但我们实际上从未真正改变他。Rust 会就此发出告警:
```console ```console
$ cargo build $ cargo build
Compiling rustfix_demo v0.1.0 (/home/lenny.peng/rust-lang-zh_CN/rustfix_demo) Compiling myprogram v0.1.0 (/home/hector/rust-lang-zh_CN/projects/myprogram)
warning: unused variable: `i` warning: variable does not need to be mutable
--> src/main.rs:4:9 --> src/main.rs:2:9
| |
4 | for i in 0..100 { 2 | let mut x = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_i` | ----^
| |
| help: remove this `mut`
| |
= note: `#[warn(unused_variables)]` on by default = note: `#[warn(unused_mut)]` (part of `#[warn(unused)]`) on by default
warning: `rustfix_demo` (bin "rustfix_demo") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
``` ```
这个告警建议咱们要使用 `_i` 做名字:其中的下划线表示咱们有意不使用这个变量。通过运行 `cargo fix` 命令,咱们就可以使`rustfix`自动应用那项建议: 这个告警建议我们移除 `mut` 关键字。我们可以通过运行 `cargo fix` 命令,`rustfix` 工具自动应用这一建议:
```console ```console
$ cargo fix --allow-no-vcs $ cargo fix --allow-no-vcs
Checking rustfix_demo v0.1.0 (/home/lenny.peng/rust-lang-zh_CN/rustfix_demo) Checking myprogram v0.1.0 (/home/hector/rust-lang-zh_CN/projects/myprogram)
Fixed src/main.rs (1 fix) Fixed src/main.rs (1 fix)
Finished dev [unoptimized + debuginfo] target(s) in 0.17s Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
``` ```
当咱们再次看到 `src/main.rs`,就将发现 `cargo fix` 已修改了这段代码: > **译注**:之所以加上 `--allow-no-vcs` 命令行开关,是因为在不加时会报出如下错误。
>
> ```console
> $ cargo fix
> error: the working directory of this package has uncommitted changes, and `cargo fix` can potentially perform destructive changes; if you'd like to suppress this error pass `--allow-dirty`, or commit the changes to these files:
>
> * projects/myprogram/ (dirty)
> * src/SUMMARY.md (dirty)
> * src/appendix/dev_tools.md (dirty)
> ```
文件名:`src/main.rs` 当我们再次看到 `src/main.rs` 时,将发现 `cargo fix` 已修改了代码:
文件名:`projects/myprogram/src/main.rs`
```rust ```rust
fn do_something() {}
fn main() { fn main() {
for _i in 0..100 { let x = 42;
do_something(); println!("{x}");
}
} }
``` ```
那个 `for` 循环变量,现在就被命名为了 `_i`,同时那条告警也不再出现 变量 `x` 现在是不可变的,并且告警也不再出现。
咱们还可使用 `cargo fix` 命令,将咱们的代码在不同 Rust 版本之间转换。有关这些 Rust 版本,在附录 E 中有讲到 咱们还可使用 `cargo fix` 命令在不同 Rust 版本之间转换代码。版本在 [附录 E](./editions.md) 中得以介绍
## 使用 Clippy 获得更多代码静态分析 ## Clippy 下的更多代码静态分析
**More Lints with Clippy** [Clippy 工具](https://github.com/rust-lang/rust-clippy) 是一个用于分析代码的静态分析工具的集合,以便咱们可以发现常见错误,仅而改进 Rust 代码。
Clippy 工具是用于分析咱们代码,从而咱们可以捕获到一些常见错误,而改进咱们 Rust 代码的一套代码静态分析集合。 要对任何 Cargo 项目运行 Clippy 的静态分析工具,请输入以下命令:
要安装 Clippy请输入以下命令
```console
$ rustup component add Clippy
```
在任何 Cargo 项目上要运行 Clippy 的静态分析,请输入以下命令:
```console ```console
$ cargo clippy $ cargo clippy
``` ```
比如说咱们编写了像下面这个程序这样,用到某个数学常量近似值,好比说 `pi`,的一个程序 例如,假设咱们编写了个程序,使用某个数学常数,比如 π 的近似值,就像下面这个程序所做的那样
文件名:`src/main.rs` 文件名:`src/main.rs`
@@ -119,11 +104,11 @@ fn main() {
} }
``` ```
这个项目上运行 `cargo clippy` 会得到下面报错: 这个项目上运行 `cargo clippy` 会得到下面这个报错:
```console ```console
$ cargo clippy $ cargo clippy
Checking clippy_demo v0.1.0 (/home/lenny.peng/rust-lang-zh_CN/clippy_demo) Checking clippy_demo v0.1.0 (/home/hector/rust-lang-zh_CN/projects/clippy_demo)
error: approximate value of `f{32, 64}::consts::PI` found error: approximate value of `f{32, 64}::consts::PI` found
--> src/main.rs:2:13 --> src/main.rs:2:13
| |
@@ -131,19 +116,23 @@ error: approximate value of `f{32, 64}::consts::PI` found
| ^^^^^^ | ^^^^^^
| |
= help: consider using the constant directly = help: consider using the constant directly
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#approx_constant
= note: `#[deny(clippy::approx_constant)]` on by default = note: `#[deny(clippy::approx_constant)]` on by default
error: could not compile `clippy_demo` due to previous error error: could not compile `clippy_demo` (bin "clippy_demo") due to 1 previous error
``` ```
报错让咱们明白Rust 已经定义了个更精确的 `PI` 常量,且当咱们使用这个常量时,咱们的程序将更为正确。那么咱们随后就应修改咱们代码为使用这个 `PI` 常量。下面的代码就捕获导致 Clippy 的任何错误或告警: 这个报错让咱们知道Rust 已经定义了个更精确的 `PI` 常量,并且若咱们使用这个常量时,程序将更为正确。因此,咱们就要修改代码为使用 `PI` 常量。
文件名:`src/main.rs` 以下代码不会导致来自 Clippy 的任何报错或告警:
文件名:`projects/clippy_demo/src/main.rs`
```rust ```rust
use std::f64::consts;
fn main() { fn main() {
let x = std::f64::consts::PI; let x = consts::PI;
let r = 8.0; let r = 8.0;
println!("圆的面积为 {}", x * r * r); println!("圆的面积为 {}", x * r * r);
} }
@@ -152,14 +141,11 @@ fn main() {
有关 Clippy 的更多信息,请参阅 [其文档](https://github.com/rust-lang/rust-clippy)。 有关 Clippy 的更多信息,请参阅 [其文档](https://github.com/rust-lang/rust-clippy)。
## 用 `rust-analyzer` 的 IDE 集成 ## 使用 `rust-analyzer` 的 IDE 集成
**IDE Integration Using `rust-analyzer`** 为了帮助 IDE 集成Rust 社区建议使用 [`rust-analyzer`](https://rust-analyzer.github.io/)。这个工具是一组以编译器为中心的实用程序,支持 [语言服务器协议Language Server Protocol](http://langserver.org/) ,该协议是 IDE 和编程语言之间相互通信的规范。不同客户端均可以使用 `rust-analyzer`,比如 [Visual Studio Code 的 Rust 分析器插件](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)。
请访问 `rust-analyzer` 项目 [主页](https://rust-analyzer.github.io/) 查看安全说明,随后在咱们的特定 IDE 中安装语言服务器支持。咱们的 IDE 将获得自动补全、跳转到定义以及内联报错等能力。
为帮助 IDE 集成Rust 社区建议使用 [`rust-analyzer`](https://rust-analyzer.github.io/)。此工具是一套以编译器为中心,操 [语言服务器协议Language Server Protocol](http://langserver.org/) 的实用工具;而所谓语言服务器协议,则是用于各种 IDEs 和编程语言,二者相互之间通信的一种规格。有多种不同客户端可使用 `rust-analyzer`,比如 [Visual Studio Code 的 Rust 分析器插件](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)。
请访问 `rust-analyzer` 项目 [主页](https://rust-analyzer.github.io/),了解其安全说明,随后在咱们的特定 IDE 中安装该语言的服务器支持。咱们的 IDE 就能获得诸如自动补全、跳至定义及行内报错等能力。
End End