分类

常用的 cargo 命令以及扩展命令

2019-07-22 00:07 rust

熟练 cargo 工具以及扩展之后, 编写 rust 软件可以更高效.

cargo 自带命令

cargo update

更新 Cargo.lock 中定义的依赖包的版本.

cargo vendor

将项目的依赖包都缓存到本项目的 vendor 目录. 这个特性对于 CI/CD 很有用处, 不再需要在每次自动编译时重新从 crates.io 下载源代码.

使用方法也很简单, 只需要在根目录运行 cargo vendor 命令即可. 然后该命令会提醒 我们修改本项目的 cargo config 文件:

$ mkdir .cargo
$ echo > .cargo/config << EOF
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
EOF

但是, 如果项目中依赖的包比较多的话, 最终 vendor/ 可能会比较大, 比如我现在调试的 一个服务端项目, 其 vendor 目录竟然有 200M, 里面有 269 个子目录.

cargo-edit

这个库提供了三个命令:

  • cargo add, 向 Cargo.toml 加入新的包
  • cargo rm, 从 Cargo.toml 中移除包
  • cargo upgrade, 升级 Cargo.toml 中的包到最新版, 同时会修改 Cargo.toml 中定义的依赖包的版本号. 与之相对应的 cargo update 命令只会更新 Cargo.lock 这个文件中定义的版本.

cargo-make

cargo-outdated

用于展示工作区中有哪些依赖包是过期的.

$ cargo outdated
Name                            Project  Compat  Latest  Kind    Platform
----                            -------  ------  ------  ----    --------
miniz_oxide_c_api->miniz_oxide  0.2.2    0.2.3   0.2.3   Normal  ---

cargo-script

直接以脚本的方式运行 rust 程序. 安装:

$ cargo install cargo-script

比如, time.rs 文件的内容是:

use std::time::SystemTime;

fn main() {
  println!("now: {:?}", SystemTime::now());
}

现在执行这个文件:

$ cargo script time

cargo-watch

用于监控源码目录的更改, 并运行指定的 cargo 命令.

比如, 代码更改后自动重新编译并运行服务器代码:

$ cargo watch -x 'run --bin http_server'

该命令可以方便地与 vim/emacs 以及其它 IDE 集成.

clippy

clippy 是非常常用的 rust lint 工具, 支持超过 300 条规则.

使用 rustup 命令来安装:

$ rustup update
$ rustup component add clippy

安装完成之后, cargo 就多 clippy 命令, 在项目目录里执行:

$ cargo clippy

就可以检验代码的问题了.

cargo audit

audit 工具可以检测 rust 项目中的依赖包是否有已知安全漏洞.

安装:

$ cargo install cargo-audit

然后在 rust 项目根目录运行一下:

$ cargo audit

即可扫描项目中的包依赖安全问题, 并生成报告.

cargo tree

tree 命令可以树形输入 crate 依赖关系.

安装:

$ cargo install cargo-tree

然后在项目根目录运行:

$ cargo tree
musl-demo v0.1.0
├── openssl v0.10.24
│   ├── bitflags v1.1.0
│   ├── cfg-if v0.1.9
│   ├── foreign-types v0.3.2
│   │   └── foreign-types-shared v0.1.1
│   ├── lazy_static v1.2.0
│   ├── libc v0.2.60
│   └── openssl-sys v0.9.48
│       └── libc v0.2.60 (*)
│       [build-dependencies]
│       ├── autocfg v0.1.5
│       ├── cc v1.0.38
│       └── pkg-config v0.3.15
└── rust_sodium v0.10.2
    ├── libc v0.2.60 (*)
    ├── rand v0.4.6
    │   └── libc v0.2.60 (*)
    ├── rust_sodium-sys v0.10.4
    │   ├── lazy_static v1.2.0 (*)
    │   ├── libc v0.2.60 (*)
    │   ├── rand v0.4.6 (*)
    │   └── unwrap v1.2.1
    │   [build-dependencies]
    │   ├── cc v1.0.38 (*)
    │   ├── flate2 v1.0.9
    │   │   ├── crc32fast v1.2.0
    │   │   │   └── cfg-if v0.1.9 (*)
    │   │   ├── libc v0.2.60 (*)
    │   │   ├── miniz-sys v0.1.12
    │   │   │   └── libc v0.2.60 (*)
    │   │   │   [build-dependencies]
    │   │   │   └── cc v1.0.38 (*)
    │   │   └── miniz_oxide_c_api v0.2.3
    │   │       ├── crc32fast v1.2.0 (*)
    │   │       ├── libc v0.2.60 (*)
    │   │       └── miniz_oxide v0.3.0
    │   │           └── adler32 v1.0.3
    │   │       [build-dependencies]
    │   │       └── cc v1.0.38 (*)
    │   ├── http_req v0.2.1
    │   │   └── native-tls v0.2.3
    │   │       ├── log v0.4.8
    │   │       │   └── cfg-if v0.1.9 (*)
    │   │       ├── openssl v0.10.24 (*)
    │   │       ├── openssl-probe v0.1.2
    │   │       └── openssl-sys v0.9.48 (*)
    │   ├── libc v0.2.60 (*)
    │   ├── pkg-config v0.3.15 (*)
    │   ├── sha2 v0.7.1
    │   │   ├── block-buffer v0.3.3
    │   │   │   ├── arrayref v0.3.5
    │   │   │   └── byte-tools v0.2.0
    │   │   ├── byte-tools v0.2.0 (*)
    │   │   ├── digest v0.7.6
    │   │   │   └── generic-array v0.9.0
    │   │   │       └── typenum v1.10.0
    │   │   └── fake-simd v0.1.2
    │   │   [dev-dependencies]
    │   │   └── digest v0.7.6 (*)
    │   ├── tar v0.4.26
    │   │   ├── filetime v0.2.6
    │   │   │   ├── cfg-if v0.1.9 (*)
    │   │   │   └── libc v0.2.60 (*)
    │   │   ├── libc v0.2.60 (*)
    │   │   └── xattr v0.2.2
    │   │       └── libc v0.2.60 (*)
    │   ├── unwrap v1.2.1 (*)
    │   └── zip v0.3.3
    │       ├── bzip2 v0.3.3
    │       │   ├── bzip2-sys v0.1.7
    │       │   │   └── libc v0.2.60 (*)
    │       │   │   [build-dependencies]
    │       │   │   └── cc v1.0.38 (*)
    │       │   └── libc v0.2.60 (*)
    │       ├── flate2 v1.0.9 (*)
    │       ├── msdos_time v0.1.6
    │       │   └── time v0.1.42
    │       │       └── libc v0.2.60 (*)
    │       │       [dev-dependencies]
    │       │       └── winapi v0.3.7
    │       ├── podio v0.1.6
    │       └── time v0.1.42 (*)
    ├── serde v1.0.98
    └── unwrap v1.2.1 (*)

cargo count

count 命令用于统计 rust 源代码行数, 安装:

$ cargo install cargo-count

查看一下当前项目:

$ cargo count --all
Gathering information...
         Language  Files  Lines  Blanks  Comments  Code
         --------  -----  -----  ------  --------  ----
         TOML      1      14     2       1         11
         D         4      30     6       0         24
         Rust      29     10994  1104    956       8934
         Python    2      79     24      8         47
         --------  -----  -----  ------  --------  ----
Totals:            36     11117  1136    965       9016

cargo license

用于列举出项目的依赖包的授权协议. 安装:

$ cargo install cargo-license

举例来看:

$ cargo license
Apache-2.0 (1): openssl
Apache-2.0/MIT (42): rand_hc, autocfg, tar, rand_core, bzip2-sys, pkg-config, flate2, security-frame
work, cc, lazy_static, c2-chacha, core-foundation-sys, xattr, remove_dir_all, winapi-i686-pc-windows
-gnu, ppv-lite86, time, rand_core, winapi-x86_64-pc-windows-gnu, cfg-if, fake-simd, bitflags, block-
buffer, typenum, digest, native-tls, miniz-sys, sha2, winapi, rand, foreign-types, bzip2, security-f
ramework-sys, core-foundation, openssl-probe, rand_chacha, rand, filetime, vcpkg, foreign-types-shar
ed, byte-tools, rand_core
BSD-2-Clause (1): arrayref
BSD-3-Clause AND Zlib (1): adler32
ISC (1): rdrand
MIT (8): openssl-sys, schannel, http_req, generic-array, zip, miniz_oxide, redox_syscall, miniz_oxid
e_c_api
MIT OR Apache-2.0 (9): crc32fast, msdos_time, rust_sodium, tempfile, libc, podio, getrandom, log, se
rde
MIT OR BSD-3-Clause (2): unwrap, rust_sodium-sys
N/A (2): musl-demo, fuchsia-cprng

rustfix

常用的代码自动修正工具, 基于 Rust 编译器以及像 clippy 这样的工具的提示.

$ cargo install cargo-fix

其他

  • cargo-husky
  • cargo-web, 将 rust 生成客户端 web 程序的工具, 使用 wasm
  • cargo-asm, 展示 rust 函数生成的汇编代码
  • cargo-bloat, 计算 rust 各部分代码在可执行文件中占的空间大小
  • cargo-inspect
  • cargo-binutils
  • cargo-sweep
  • cargo-geiger

参考

  • https://doc.rust-lang.org/cargo/
  • https://github.com/rust-lang/rust-clippy
  • https://github.com/RustSec/cargo-audit
  • https://rustsec.org/
  • https://github.com/koute/cargo-web
  • https://github.com/DanielKeep/cargo-script
  • https://github.com/sfackler/cargo-tree
  • https://github.com/kbknapp/cargo-count
  • https://github.com/onur/cargo-license
  • https://github.com/RazrFalcon/cargo-bloat
  • https://github.com/gnzlbg/cargo-asm
  • https://github.com/svenstaro/cargo-profiler
  • https://github.com/rust-lang-nursery/rustfix
  • https://github.com/rust-embedded/cargo-binutils
  • https://github.com/mre/cargo-inspect
  • https://github.com/holmgr/cargo-sweep
  • https://github.com/anderejd/cargo-geiger