cargo --list命令详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
➜  ~ cargo --list
Installed Commands:
add Add dependencies to a Cargo.toml manifest file
b alias: build
bench Execute all benchmarks of a local package
build Compile a local package and all of its dependencies
c alias: check
cache
check Check a local package and all of its dependencies for errors
clean Remove artifacts that cargo has generated in the past
clippy Checks a package to catch common mistakes and improve your Rust code.
config Inspect configuration values
d alias: doc
doc Build a package's documentation
fetch Fetch dependencies of a package from the network
fix Automatically fix lint warnings reported by rustc
fmt Formats all bin and lib files of the current crate using rustfmt.
generate-lockfile Generate the lockfile for a package
git-checkout REMOVED: This command has been removed
hello
help Displays help for a cargo subcommand
info Display information about a package
init Create a new cargo package in an existing directory
install Install a Rust binary
locate-project Print a JSON representation of a Cargo.toml file's location
login Log in to a registry.
logout Remove an API token from the registry locally
metadata Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format
miri
new Create a new cargo package at <path>
owner Manage the owners of a crate on the registry
package Assemble the local package into a distributable tarball
pkgid Print a fully qualified package specification
publish Upload a package to the registry
r alias: run
read-manifest DEPRECATED: Print a JSON representation of a Cargo.toml manifest.
remove Remove dependencies from a Cargo.toml manifest file
report Generate and display various kinds of reports
rm alias: remove
run Run a binary or example of the local package
rustc Compile a package, and pass extra options to the compiler
rustdoc Build a package's documentation, using specified custom flags.
search Search packages in the registry. Default registry is crates.io
set-version
t alias: test
test Execute all unit and integration tests and build examples of a local package
tree Display a tree visualization of a dependency graph
uninstall Remove a Rust binary
update Update dependencies as recorded in the local lock file
upgrade
vendor Vendor all dependencies for a project locally
verify-project DEPRECATED: Check correctness of crate manifest.
version Show version information
yank Remove a pushed crate from the index


我来详细解释一下 Cargo 的所有命令:

  1. 基础构建命令
  • cargo build(简写:b):编译当前包和所有依赖
  • cargo check(简写:c):检查代码是否能编译,比 build 快
  • cargo run(简写:r):编译并运行当前包
  • cargo test(简写:t):运行所有测试
  • cargo bench:执行所有基准测试
  1. 项目管理
  • cargo new:在指定路径创建新的 Cargo 项目
  • cargo init:在现有目录中初始化 Cargo 项目
  • cargo add:添加依赖到 Cargo.toml
  • cargo remove(简写:rm):从 Cargo.toml 移除依赖
  • cargo update:更新依赖版本
  • cargo clean:删除生成的构建文件
  1. 代码质量与格式化
  • cargo clippy:代码静态分析工具,检查常见错误
  • cargo fmt:使用 rustfmt 格式化代码
  • cargo fix:自动修复 rustc 报告的警告
  • cargo miri:解释器,用于检测未定义行为
  1. 文档相关
  • cargo doc(简写:d):生成项目文档
  • cargo rustdoc:使用自定义标志构建文档
  1. 依赖管理
  • cargo fetch:下载依赖,但不编译
  • cargo vendor:将所有依赖本地化
  • cargo tree:显示依赖关系树
  • cargo generate-lockfile:生成锁文件
  1. 发布相关
  • cargo publish:发布包到 crates.io
  • cargo package:将包打包成可分发的 tarball
  • cargo yank:从索引中撤回已发布的版本
  • cargo login:登录到注册表
  • cargo logout:注销注册表令牌
  • cargo owner:管理 crate 的所有者
  1. 信息查询
  • cargo search:在注册表中搜索包
  • cargo version:显示版本信息
  • cargo metadata:输出依赖解析信息
  • cargo info:显示包信息
  • cargo pkgid:打印完整的包规格
  • cargo locate-project:打印 Cargo.toml 位置
  • cargo config:检查配置值
  • cargo report:生成各种报告
  1. 其他工具
  • cargo install:安装 Rust 二进制程序
  • cargo uninstall:卸载 Rust 二进制程序
  • cargo rustc:编译包并传递额外选项给编译器
  1. 已弃用命令
  • verify-project:检查清单正确性(已弃用)
  • read-manifest:打印 Cargo.toml 的 JSON 表示(已弃用)
  • git-checkout:已移除

每个命令都有其特定的用途和选项,可以通过 cargo help <command> 查看具体命令的详细帮助信息。这些命令共同构成了 Rust 的包管理和构建系统,使得项目管理、依赖处理、测试和发布变得更加方便。

文章目录