此快速参考备忘清单 Cargo 提供了编译 Rust 常用命令和示例
入门
安装/升级 Rust 和 Cargo
1
| $ curl -sSf https://static.rust-lang.org/rustup.sh | sh
|
在 Linux 和 macOS 系统上,这可以通过以上方命令完成
命令说明
| :- |
:- |
cargo version |
显示版本信息以确认 Cargo 已安装 |
cargo new |
创建一个新项目 |
cargo test |
在项目中运行单元测试 |
cargo check |
快速编译项目,无需生成二进制文件来检查错误 |
cargo fmt |
自动格式化代码 |
cargo build |
编译一个项目 |
cargo run |
一步编译和运行项目 |
cargo clippy --all-targets -- --D warnings |
Linter 检查错误 |
cargo tarpaulin --ignore-tests |
检查代码覆盖率 |
切换源
1 2
| $ touch ~/.cargo/config $ vim ~/.cargo/config
|
配置文件 config 内容
1 2 3 4 5 6 7
| [source.crates-io] registry = "https://github.com/rust-lang/crates.io-index" replace-with = 'tuna'
[source.tuna] registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
|
💥 注意切换源需要删除缓存目录
1
| $ rm -rf ~/.cargo/.package-cache
|
创建新项目
1
| $ cargo new hello_world --bin
|
--bin 正在制作一个二进制程序
--lib 正在创建一个库(lib)
得到如下目录:
1 2 3 4 5 6
| $ cd hello_world $ tree . . ├── Cargo.toml └── src └── main.rs
|
Cargo.toml 被称为一个 manifest 元清单,它包含了 Cargo 编译项目所需的所有元数据
1 2 3 4 5 6 7
| [package] name = "hello_world" version = "0.1.0" authors = ["Your Name <you@example.com>"] edition = "2018"
[dependencies]
|
入口文件 src/main.rs
1 2 3
| fn main() { println!("Hello, world!"); }
|
运行编译生成 hello_world 二进制文件
1 2 3
| $ cargo build
$ cargo build --release
|
然后运行它:
1 2
| $ ./target/debug/hello_world Hello, world!
|
也可以直接使用 cargo run,它会自行编译并运行它:
1 2 3
| $ cargo run Running `target/hello_world` Hello, world!
|
来源配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| [source]
[source.my-awesome-source] directory = "vendor"
git = "https://example.com/path/to/repo"
[source.crates-io] replace-with = "my-awesome-source"
|
编译测试
1 2 3 4 5
| $ cargo build
$ cargo build --release $ cargo run
|
测试
1 2 3 4 5 6 7 8
| $ cargo test
$ cargo test test_foo
$ cargo test --package rustt --lib -- foo::bar::tests::test_foo --exact --nocapture
$ cargo test --package rustt --lib -- foo::bar::tests --nocapture
|
配置目标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| [package]
[lib]
name = "foo"
path = "src/lib.rs"
test = true
doctest = true
bench = true
doc = true
plugin = false
proc-macro = false
harness = true
edition = '2015'
|
项目目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| . ├── Cargo.lock ├── Cargo.toml ├── benches │ └── large-input.rs ├── examples │ └── simple.rs ├── src │ ├── bin │ │ └── another_executable.rs │ ├── lib.rs │ └── main.rs └── tests └── some-integration-tests.rs
|
配置
1 2 3 4 5 6 7 8 9 10
| [source.the-source-name]
replace-with = "another-source"
registry = "https://example.com/path/to/index" local-registry = "path/to/registry" directory = "path/to/vendor"
|
更换源的配置通过完成 .cargo/config,上面是全套可用字段
包相关命令
init/new
创建一个新的 cargo 包
1 2
| $ cargo init [options] [path] $ cargo new [options] [path]
|
--bin 创建具有二进制目标 (src/main.rs) 的包(默认行为)
--lib 使用库目标 (src/lib.rs) 创建一个包
--edition edition 指定要使用的 Rust 版本。默认值为 2021。可能的值:2015、2018、2021
--name name 设置包名。默认为目录名称
--vcs vcs 为给定的版本控制系统(git、hg、pijul 或fossil)初始化一个新的 VCS 存储库,或者根本不初始化任何版本控制(无)
--registry registry 限制仅发布到该注册表
安装包
1 2 3 4 5 6
| $ cargo install ripgrep
$ cargo install --path .
$ cargo install --list
|
--vers version
--version version 指定要安装的版本
--git url 用于安装指定 crate 的 Git URL
--branch branch 从 git 安装时要使用的分支
--tag tag 从 git 安装时使用的标记
--rev sha 从 git 安装时使用的特定提交
--path path 要安装的本地 crate 的文件系统路径
--list 列出所有已安装的软件包及其版本
-f, --force 强制覆盖现有的 crate 或二进制文件
搜索包
1
| $ cargo search [options] [query...]
|
--limit limit 限制结果数量(默认值:10,最大值:100)
--index index 要使用的注册表索引的 URL
--registry registry 要使用的注册表的名称
卸载包
1
| $ cargo uninstall [options] [spec...]
|
-p, --package spec... 要卸载的软件包
--bin name... 仅卸载二进制名称
--root dir 从中卸载软件包的目录
1
| $ cargo uninstall ripgrep
|
发布命令
登录
1
| $ cargo login [options] [token]
|
| :- |
:- |
--registry |
要使用的注册表的名称 |
| :- |
:- |
-v, --verbose |
启用更加详细的输出 |
-q, --quiet |
不输出Cargo的日志信息 |
--color when |
输出内容的颜色 auto, always, never |
所有者
1 2 3 4 5 6
| $ cargo owner --list foo
$ cargo owner --add username foo
$ cargo owner --remove username foo
|
| :- |
:- |
--token token |
身份验证时使用的 API 令牌 |
--index index |
要使用的注册表索引的 URL |
打包 & 发布公共选项
选择包
-p spec..., --package spec... Package 指定包
--workspace Package 工作区中的全体成员
--exclude SPEC... 排除指定包
编译选项
--target triple 为指定架构执行 Package
--target-dir directory 用于存放生成的工件以及中间文件的目录
特性选择
--features features 传递以空格或者逗号分隔的列表,其中给出要启用的特性
--all-features 为给定的包启用全部可用特性
--no-default-features 不启用给定包的default特性
清单选项
--manifest-path path 用于指定 Cargo.toml 文件的路径
--frozen, --locked 这两个选项用于保证Cargo.lock文件是最新的
--offline 禁止Cargo访问网络
混杂选项
-j N, --jobs N 要并行运行的作业数量
打包
将本地包打包为可分发的压缩文件
1
| $ cargo package [options]
|
-l, --list 输出包中包含的文件(不实际进行打包)。
--no-verify 构建包时不进行校验。
--no-metadata 忽略 缺少可读的元信息(如描述信息或采用的授权协议) 时产生的警告。
--allow-dirty 允许打包 在版本控制系统中仍有未提交内容 的包。
发布
1
| $ cargo publish [options]
|
发布选项
--dry-run 在不上传的情况下执行所有检查
--token token 身份验证时使用的 API 令牌
--no-verify 不要通过构建内容来验证内容
--allow-dirty 允许打包具有未提交的 VCS 更改的工作目录
--index index 要使用的注册表索引的 URL
--registry registry 要发布到的注册表的名称
打包选择
-p spec, --package spec 要发布的包
yank
从服务器的索引中删除以前发布的 crate 版本
1
| $ cargo yank --vers 1.0.7 foo
|
--vers version 要 yank 或 un-yank 的版本
--undo 撤消 yank,将版本放回索引中
--token token 身份验证时使用的 API 令牌
--index index 要使用的注册表索引的 URL
--registry registry 要使用的注册表名称
另见