Cargo 命令速查

Cargo 项目管理、构建、测试、发布、依赖管理的完整命令速查。

#tech / dev / rust #type / howto #status / evergreen #resource / cargo

[!info] related notes

Cargo 命令速查

核心概念

Cargo.toml          项目清单(元数据 + 依赖声明)
Cargo.lock          精确版本锁定(类似 package-lock.json)
target/             构建产物目录
crates.io           官方包注册中心

项目创建与初始化

命令说明常用示例
cargo new <name>创建二进制项目(可执行程序)cargo new my-app
cargo new --lib <name>创建库项目(纯 Rust 库)cargo new --lib my-lib
cargo new --vcs git <name>创建项目并初始化 Gitcargo new --vcs git my-app
cargo new --edition <year> <name>指定 Rust 版本cargo new --edition 2024 my-app
cargo init在当前目录初始化项目cargo init(二进制)
cargo init --lib在当前目录初始化库项目cargo init --lib

构建与运行

命令说明常用示例
cargo build调试构建(输出到 target/debug/cargo build
cargo build --release发布构建(开启优化,输出到 target/release/cargo build --release
cargo run构建并运行cargo run
cargo run -- <args>运行时传递参数cargo run -- arg1 arg2
cargo run --release以发布模式运行cargo run --release
cargo run --bin <name>运行指定二进制目标cargo run --bin my-tool
cargo run --example <name>运行示例代码cargo run --example hello
cargo check快速语法/类型检查(不生成二进制)cargo check(开发时推荐,比 build 快很多)
cargo clean清理 target 目录cargo clean

测试与基准

命令说明常用示例
cargo test运行所有测试cargo test
cargo test <name>运行名称匹配的测试cargo test test_parse
cargo test -- --nocapture显示 println! 输出cargo test -- --nocapture
cargo test -- --test-threads=1单线程运行测试cargo test -- --test-threads=1
cargo test --lib只运行库测试cargo test --lib
cargo test --doc只运行文档测试cargo test --doc
cargo test --release以发布模式运行测试cargo test --release
cargo bench运行基准测试cargo bench
cargo nextest run使用 nextest 运行测试(更快,并行更好)需先 cargo install cargo-nextest

依赖管理

命令说明常用示例
cargo add <crate>添加依赖cargo add serde
cargo add <crate>@<version>添加指定版本依赖cargo add serde@1.0
cargo add <crate> --features <f>添加带特性的依赖cargo add tokio --features full
cargo add <crate> --dev添加为开发依赖cargo add mockall --dev
cargo add <crate> --build添加为构建依赖cargo add cc --build
cargo add <crate> --rename <alias>添加并重命名cargo add my-crate --rename mc
cargo remove <crate>移除依赖cargo remove serde
cargo update更新所有依赖(更新 Cargo.lock)cargo update
cargo update <crate>更新指定依赖cargo update serde
cargo update --dry-run预览更新(不实际修改)cargo update --dry-run
cargo tree查看依赖树cargo tree
cargo tree -d查看重复依赖cargo tree -d
cargo tree -i <crate>查看谁依赖了指定包cargo tree -i serde
cargo tree --depth <n>限制依赖树深度cargo tree --depth 2
cargo outdated检查过时依赖需先 cargo install cargo-outdated

文档

命令说明常用示例
cargo doc生成文档cargo doc
cargo doc --open生成并在浏览器打开cargo doc --open
cargo doc --document-private-items包含私有项的文档cargo doc --document-private-items

发布与安装

命令说明常用示例
cargo publish发布到 crates.iocargo publish
cargo publish --dry-run预览发布内容cargo publish --dry-run
cargo package打包但不发布cargo package
cargo package --list查看打包包含的文件cargo package --list
cargo install <crate>从 crates.io 安装二进制 cratecargo install ripgrep
cargo install --path .从本地项目安装cargo install --path .
cargo install --list列出已安装的 cratecargo install --list
cargo uninstall <crate>卸载已安装的 cratecargo uninstall ripgrep

工作区 (Workspace)

命令说明常用示例
cargo build -p <member>构建指定工作区成员cargo build -p core
cargo test -p <member>测试指定工作区成员cargo test -p cli
cargo test --workspace测试所有工作区成员cargo test --workspace
cargo build --workspace构建所有工作区成员cargo build --workspace

工作区配置示例(根目录 Cargo.toml):

[workspace]
members = [
    "crates/core",
    "crates/cli",
    "crates/web",
]
resolver = "2"

特性 (Features) 管理

命令说明常用示例
cargo build --features <f>启用指定特性cargo build --features json
cargo build --no-default-features禁用默认特性cargo build --no-default-features
cargo build --features "f1,f2"启用多个特性cargo build --features "json,yaml"

特性配置示例(Cargo.toml):

[features]
default = ["sqlite"]
sqlite = ["rusqlite"]
postgres = ["tokio-postgres"]
json = ["serde_json"]

[dependencies]
rusqlite = { version = "0.27", optional = true }
tokio-postgres = { version = "0.7", optional = true }
serde_json = { version = "1.0", optional = true }

配置速查

配置项说明推荐值
[package] name包名(crates.io 唯一)snake_case
[package] version语义化版本0.1.0
[package] editionRust 版本20212024
[dependencies]运行时依赖serde = "1.0"
[dev-dependencies]开发/测试依赖mockall = "0.11"
[build-dependencies]构建脚本依赖cc = "1.0"
[profile.dev]开发构建配置opt-level = 1(加速编译)
[profile.release]发布构建配置lto = true(链接时优化)
[profile.release] strip去除调试符号true(减小二进制体积)

常用插件

插件用途安装命令
cargo-edit扩展 add/rm/upgrade 命令cargo install cargo-edit
cargo-watch文件变化自动重建/测试cargo install cargo-watch
cargo-watch 用法监听变化并运行cargo watch -x check -x test
cargo-watch 用法监听变化并运行程序cargo watch -x run
cargo-audit检查依赖安全漏洞cargo install cargo-audit && cargo audit
cargo-expand展开宏查看生成代码cargo install cargo-expand
cargo-bloat分析二进制体积cargo install cargo-bloat
cargo-nextest更快的测试运行器cargo install cargo-nextest
cargo-outdated检查过时依赖cargo install cargo-outdated
cargo-clippyRust linter(随 rustup 自带)cargo clippy
cargo-fmt代码格式化(随 rustup 自带)cargo fmt

常见问题速查

问题原因解决方案
编译缓慢默认优化级别高或依赖多开发时用 cargo check 代替 cargo build
下载依赖慢网络问题配置国内镜像源(中科大/字节)
版本冲突依赖版本不兼容cargo tree -d 查看冲突,用 [patch] 覆盖
内存不足并行编译占用太多CARGO_BUILD_JOBS=2 cargo build
cargo build 报链接错误系统缺少链接器或库安装对应系统依赖(如 pkg-configopenssl-dev
二进制体积过大包含调试符号profile.release 中设置 strip = truelto = true
Cargo.lock 冲突Git 合并时冲突删除 Cargo.lock 重新 cargo build,或 cargo update
创建于 2026/7/6 更新于 2026/7/15