附录 D:实用开发工具

appendix-04-useful-development-tools.md
commit efbafdba3618487fbc9305318fcab9775132ac15

本附录,我们将讨论 Rust 项目提供的用于开发 Rust 代码的工具。

通过 rustfmt 自动格式化

rustfmt 工具根据社区代码风格格式化代码。很多项目使用 rustfmt 来避免编写 Rust 风格的争论:所有人都用这个工具格式化代码!

安装 rustfmt

  1. $ rustup component add rustfmt

这会提供 rustfmtcargo-fmt,类似于 Rust 同时安装 rustccargo。为了格式化整个 Cargo 项目:

  1. $ cargo fmt

运行此命令会格式化当前 crate 中所有的 Rust 代码。这应该只会改变代码风格,而不是代码语义。请查看 该文档 了解 rustfmt 的更多信息。

通过 rustfix 修复代码

如果你编写过 Rust 代码,那么你可能见过那些有很明显修复方式的编译器警告。例如,考虑如下代码:

文件名: src/main.rs

  1. fn do_something() {}
  2. fn main() {
  3. for i in 0..100 {
  4. do_something();
  5. }
  6. }

这里调用了 do_something 函数 100 次,不过从未在 for 循环体中使用变量 i。Rust 会警告说:

  1. $ cargo build
  2. Compiling myprogram v0.1.0 (file:///projects/myprogram)
  3. warning: unused variable: `i`
  4. --> src/main.rs:4:9
  5. |
  6. 4 | for i in 0..100 {
  7. | ^ help: consider using `_i` instead
  8. |
  9. = note: #[warn(unused_variables)] on by default
  10. Finished dev [unoptimized + debuginfo] target(s) in 0.50s

警告中建议使用 _i 名称:下划线表明该变量有意不使用。我们可以通过 cargo fix 命令使用 rustfix 工具来自动采用该建议:

  1. $ cargo fix
  2. Checking myprogram v0.1.0 (file:///projects/myprogram)
  3. Fixing src/main.rs (1 fix)
  4. Finished dev [unoptimized + debuginfo] target(s) in 0.59s

如果再次查看 src/main.rs,会发现 cargo fix 修改了代码:

文件名: src/main.rs

  1. fn do_something() {}
  2. fn main() {
  3. for _i in 0..100 {
  4. do_something();
  5. }
  6. }

现在 for 循环变量变为 _i,警告也不再出现。

cargo fix 命令可以用于在不同 Rust 版本间迁移代码。版本在附录 E 中介绍。

通过 clippy 提供更多 lint 功能

clippy 工具是一系列 lint 的集合,用于捕捉常见错误和改进 Rust 代码。

安装 clippy

  1. $ rustup component add clippy

对任何 Cargo 项目运行 clippy 的 lint:

  1. $ cargo clippy

例如,如果程序使用了如 pi 这样数学常数的近似值,如下:

文件名: src/main.rs

  1. fn main() {
  2. let x = 3.1415;
  3. let r = 8.0;
  4. println!("the area of the circle is {}", x * r * r);
  5. }

在此项目上运行 cargo clippy 会导致这个错误:

  1. error: approximate value of `f{32, 64}::consts::PI` found. Consider using it directly
  2. --> src/main.rs:2:13
  3. |
  4. 2 | let x = 3.1415;
  5. | ^^^^^^
  6. |
  7. = note: #[deny(clippy::approx_constant)] on by default
  8. = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#approx_constant

这告诉我们 Rust 定义了更为精确的常量,而如果使用了这些常量程序将更加准确。如下代码就不会导致 clippy 产生任何错误或警告:

文件名: src/main.rs

  1. fn main() {
  2. let x = std::f64::consts::PI;
  3. let r = 8.0;
  4. println!("the area of the circle is {}", x * r * r);
  5. }

请查看 其文档 来了解 clippy 的更多信息。

使用 rust-analyzer 的 IDE 集成

为了帮助 IDE 集成,Rust 社区建议使用 rust-analyzer。这个工具是一组以编译器为中心的实用程序,它实现了 Language Server Protocol(一个 IDE 与编程语言之间的通信规范)。rust-analyzer 可以用于不同的客户端,比如 Visual Studio Code 的 Rust analyzer 插件

访问 rust-analyzer 项目的 主页 来了解如何安装安装它,然后为你的 IDE 安装 language server 支持。如此你的 IDE 便会获得如自动补全、跳转到定义和 inline error 之类的功能。