📦 项目管理
# 创建新项目
cargo new myproject # 创建二进制项目
cargo new --lib mylib # 创建库项目
cargo init # 在当前目录初始化项目
# 项目信息
cargo tree # 显示依赖树
cargo metadata # 输出项目元数据(JSON)
🔨 构建与编译
# 基础构建
cargo build # Debug 构建
cargo build --release # Release 构建(优化)
cargo build --target x86_64-unknown-linux-musl # 指定目标平台
# 检查(不生成二进制,速度快)
cargo check # 快速检查编译错误
cargo check --all-targets # 检查所有目标(包括 tests/benches)
# 清理
cargo clean # 删除 target 目录
🚀 运行与测试
# 运行
cargo run # 运行主程序
cargo run --release # Release 模式运行
cargo run --bin mybin # 运行指定二进制
cargo run -- arg1 arg2 # 传递参数
# 测试
cargo test # 运行所有测试
cargo test test_name # 运行特定测试
cargo test --lib # 只测试库代码
cargo test -- --nocapture # 显示 println! 输出
cargo test -- --test-threads=1 # 单线程运行测试
# 基准测试
cargo bench # 运行所有基准测试
cargo bench --bench bench_name # 运行特定基准测试
📚 依赖管理
# 添加依赖
cargo add serde # 添加依赖(需要 cargo-edit)
cargo add tokio --features full
cargo add serde_json --dev # 添加开发依赖
# 更新依赖
cargo update # 更新所有依赖(遵循 Cargo.toml)
cargo update -p serde # 更新特定包
# 移除依赖
cargo rm serde # 移除依赖(需要 cargo-edit)
# 查看依赖
cargo tree # 依赖树
cargo tree -i tokio # 反向查看谁依赖 tokio
cargo tree -d # 只显示重复依赖
📖 文档
# 生成文档
cargo doc # 生成文档
cargo doc --open # 生成并打开文档
cargo doc --no-deps # 不生成依赖的文档
# 查看标准库文档
cargo doc --open -p std
🔍 代码质量
# 格式化
cargo fmt # 格式化代码
cargo fmt -- --check # 检查格式(CI 中使用)
# Lint 检查
cargo clippy # 运行 Clippy 检查
cargo clippy -- -D warnings # 将警告视为错误
cargo clippy --fix # 自动修复问题
# 审计安全漏洞
cargo audit # 检查已知安全漏洞(需要 cargo-audit)
📦 发布与打包
# 发布到 crates.io
cargo publish # 发布包
cargo publish --dry-run # 模拟发布
cargo yank --vers 1.0.1 # 撤回版本
# 打包
cargo package # 打包(不上传)
cargo package --list # 列出打包文件
# 安装
cargo install cargo-edit # 从 crates.io 安装工具
cargo install --path . # 从本地源码安装
cargo uninstall cargo-edit # 卸载工具
🔧 高级功能
# 特性(Features)
cargo build --features "feature1 feature2"
cargo build --all-features
cargo build --no-default-features
# Workspace 管理
cargo build --workspace # 构建所有成员
cargo test --workspace # 测试所有成员
cargo build -p member_name # 构建特定成员
# 交叉编译
cargo build --target aarch64-unknown-linux-gnu
rustup target add aarch64-unknown-linux-gnu # 先添加目标
# 离线模式
cargo build --offline # 不访问网络
🛠️ 实用工具(需额外安装)
# 安装常用工具
cargo install cargo-edit # add/rm 命令
cargo install cargo-watch # 自动重新编译
cargo install cargo-expand # 展开宏
cargo install cargo-audit # 安全审计
cargo install cargo-outdated # 检查过期依赖
cargo install cargo-geiger # 检测 unsafe 代码
# 使用示例
cargo watch -x check # 文件变化时自动 check
cargo watch -x test # 自动运行测试
cargo expand # 展开宏定义
cargo outdated # 查看可更新的依赖
cargo geiger # 统计 unsafe 代码
🎯 开发常用组合
# 开发时快速迭代
cargo watch -x 'check --all-targets' -x 'test --lib'
# CI/CD 检查
cargo fmt -- --check && cargo clippy -- -D warnings && cargo test
# 发布前检查
cargo test --release && cargo doc --no-deps && cargo package --list
# 性能分析
cargo build --release && perf record target/release/myapp
cargo flamegraph # 需要 cargo-flamegraph
# 依赖分析
cargo tree -d # 重复依赖
cargo bloat --release # 二进制大小分析(需要 cargo-bloat)
📝 环境变量
# 常用环境变量
RUST_LOG=debug cargo run # 设置日志级别
RUSTFLAGS="-C target-cpu=native" cargo build --release # 优化编译
# 修改 Cargo 行为
CARGO_TARGET_DIR=/tmp/target # 改变构建目录
CARGO_HTTP_MULTIPLEXING=false # 禁用 HTTP/2
📌 配置文件位置
# 项目配置
Cargo.toml # 项目清单
Cargo.lock # 锁定依赖版本
.cargo/config.toml # 项目级配置
# 全局配置
~/.cargo/config.toml # 全局 Cargo 配置
~/.cargo/credentials.toml # crates.io 凭证
💡 实用技巧
# 查看命令帮助
cargo help # 所有命令列表
cargo help build # 特定命令帮助
# 详细输出
cargo build -vv # 超详细输出(调试构建问题)
# 仅构建依赖(Docker 层缓存优化)
cargo build --release --frozen # 不更新 Cargo.lock