Quick Reference Card
速查卡片
Cheat Sheet: Commands at a Glance
命令速查:一眼看全
# ─── Build Scripts ───
# ─── 构建脚本 ───
cargo build # Compiles build.rs first, then crate
# 先编译 build.rs,再编译当前 crate
cargo build -vv # Verbose — shows build.rs output
# 详细模式,会把 build.rs 输出也打出来
# ─── Cross-Compilation ───
# ─── 交叉编译 ───
rustup target add x86_64-unknown-linux-musl
# 添加 musl 目标
cargo build --release --target x86_64-unknown-linux-musl
# 构建静态 Linux 发布版
cargo zigbuild --release --target x86_64-unknown-linux-gnu.2.17
# 用 zig 工具链构建旧 glibc 兼容版本
cross build --release --target aarch64-unknown-linux-gnu
# 借助 cross 构建 aarch64 Linux 目标
# ─── Benchmarking ───
# ─── 基准测试 ───
cargo bench # Run all benchmarks
# 运行全部 benchmark
cargo bench -- parse # Run benchmarks matching "parse"
# 只跑名字匹配 "parse" 的 benchmark
cargo flamegraph -- --args # Generate flamegraph from binary
# 为可执行文件生成火焰图
perf record -g ./target/release/bin # Record perf data
# 采集 perf 数据
perf report # View perf data interactively
# 交互式查看 perf 结果
# ─── Coverage ───
# ─── 覆盖率 ───
cargo llvm-cov --html # HTML report
# 输出 HTML 覆盖率报告
cargo llvm-cov --lcov --output-path lcov.info
# 生成 lcov 格式报告
cargo llvm-cov --workspace --fail-under-lines 80
# 工作区覆盖率低于 80% 时失败
cargo tarpaulin --out Html # Alternative tool
# tarpaulin 的 HTML 报告模式
# ─── Safety Verification ───
# ─── 安全性验证 ───
cargo +nightly miri test # Run tests under Miri
# 在 Miri 下运行测试
MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test
# 关闭隔离限制后运行 Miri
valgrind --leak-check=full ./target/debug/binary
# 用 Valgrind 做完整泄漏检查
RUSTFLAGS="-Zsanitizer=address" cargo +nightly test -Zbuild-std --target x86_64-unknown-linux-gnu
# 开启 AddressSanitizer 运行测试
# ─── Audit & Supply Chain ───
# ─── 审计与供应链 ───
cargo audit # Known vulnerability scan
# 扫描已知漏洞
cargo audit --deny warnings # Fail CI on any advisory
# 发现 advisory 就让 CI 失败
cargo deny check # License + advisory + ban + source checks
# 检查许可证、公告、禁用项和源来源
cargo deny list # List all licenses in dep tree
# 列出依赖树中的全部许可证
cargo vet # Supply chain trust verification
# 做供应链信任校验
cargo outdated --workspace # Find outdated dependencies
# 找出过期依赖
cargo semver-checks # Detect breaking API changes
# 检测破坏性 API 变化
cargo geiger # Count unsafe in dependency tree
# 统计依赖树中的 unsafe 使用量
# ─── Binary Optimization ───
# ─── 二进制优化 ───
cargo bloat --release --crates # Size contribution per crate
# 查看各 crate 的体积贡献
cargo bloat --release -n 20 # 20 largest functions
# 列出最大的 20 个函数
cargo +nightly udeps --workspace # Find unused dependencies
# 查找未使用依赖
cargo machete # Fast unused dep detection
# 更快的未使用依赖扫描
cargo expand --lib module::name # See macro expansions
# 查看宏展开结果
cargo msrv find # Discover minimum Rust version
# 探测最低 Rust 版本
cargo clippy --fix --workspace --allow-dirty # Auto-fix lint warnings
# 自动修复可处理的 lint 警告
# ─── Compile-Time Optimization ───
# ─── 编译时间优化 ───
export RUSTC_WRAPPER=sccache # Shared compilation cache
# 启用共享编译缓存
sccache --show-stats # Cache hit statistics
# 查看缓存命中统计
cargo nextest run # Faster test runner
# 使用更快的测试执行器
cargo nextest run --retries 2 # Retry flaky tests
# 易抖测试自动重试两次
# ─── Platform Engineering ───
# ─── 平台工程 ───
cargo check --target thumbv7em-none-eabihf # Verify no_std builds
# 校验 no_std 目标能否通过检查
cargo build --target x86_64-pc-windows-gnu # Cross-compile to Windows
# 交叉编译到 Windows GNU 目标
cargo xwin build --target x86_64-pc-windows-msvc # MSVC ABI cross-compile
# 交叉编译到 Windows MSVC ABI
cfg!(target_os = "linux") # Compile-time cfg (evaluates to bool)
# 编译期 cfg 判断,结果是布尔值
# ─── Release ───
# ─── 发布 ───
cargo release patch --dry-run # Preview release
# 预览一次 patch 发布
cargo release patch --execute # Bump, commit, tag, publish
# 提升版本、提交、打 tag、发布
cargo dist plan # Preview distribution artifacts
# 预览分发产物计划
Decision Table: Which Tool When
决策表:什么目标用什么工具
| Goal | Tool | When to Use |
|---|---|---|
| Embed git hash / build info 嵌入 git hash 或构建信息 | build.rsbuild.rs | Binary needs traceability 二进制产物需要可追踪性时 |
| Compile C code with Rust 把 C 代码一起编进 Rust | cc crate in build.rsbuild.rs 里的 cc crate | FFI to small C libraries 对接小型 C 库时 |
| Generate code from schemas 从模式文件生成代码 | prost-build / tonic-buildprost-build / tonic-build | Protobuf, gRPC, FlatBuffers 处理 Protobuf、gRPC、FlatBuffers 时 |
| Link system library 链接系统库 | pkg-config in build.rsbuild.rs 中的 pkg-config | OpenSSL, libpci, systemd 例如 OpenSSL、libpci、systemd |
| Static Linux binary 静态 Linux 二进制 | --target x86_64-unknown-linux-musl--target x86_64-unknown-linux-musl | Container/cloud deployment 容器或云环境部署 |
| Target old glibc 兼容旧版 glibc | cargo-zigbuildcargo-zigbuild | RHEL 7, CentOS 7 compatibility 需要兼容 RHEL 7、CentOS 7 时 |
| ARM server binary ARM 服务器二进制 | cross or cargo-zigbuildcross 或 cargo-zigbuild | Graviton/Ampere deployment 面向 Graviton、Ampere 等部署 |
| Statistical benchmarks 统计型基准测试 | Criterion.rs Criterion.rs | Performance regression detection 监测性能回退 |
| Quick perf check 快速性能检查 | Divan Divan | Development-time profiling 开发阶段临时分析 |
| Find hot spots 定位热点 | cargo flamegraph / perfcargo flamegraph / perf | After benchmark identifies slow code benchmark 确认代码很慢之后 |
| Line/branch coverage 行覆盖率与分支覆盖率 | cargo-llvm-covcargo-llvm-cov | CI coverage gates, gap analysis CI 覆盖率门槛与缺口分析 |
| Quick coverage check 快速看覆盖率 | cargo-tarpaulincargo-tarpaulin | Local development 本地开发阶段 |
| Rust UB detection 检测 Rust UB | Miri Miri | Pure-Rust unsafe code纯 Rust 的 unsafe 代码 |
| C FFI memory safety C FFI 内存安全检查 | Valgrind memcheck Valgrind memcheck | Mixed Rust/C codebases Rust/C 混合代码库 |
| Data race detection 数据竞争检测 | TSan or Miri TSan 或 Miri | Concurrent unsafe code并发 unsafe 代码 |
| Buffer overflow detection 缓冲区溢出检测 | ASan ASan | unsafe pointer arithmetic涉及 unsafe 指针运算 |
| Leak detection 泄漏检测 | Valgrind or LSan Valgrind 或 LSan | Long-running services 长时间运行的服务 |
| Local CI equivalent 本地模拟 CI | cargo-makecargo-make | Developer workflow automation 开发流程自动化 |
| Pre-commit checks 提交前检查 | cargo-husky or git hookscargo-husky 或 git hook | Catch issues before push 在推送前拦住问题 |
| Automated releases 自动化发布 | cargo-release + cargo-distcargo-release + cargo-dist | Version management + distribution 版本管理与分发 |
| Dependency auditing 依赖审计 | cargo-audit / cargo-denycargo-audit / cargo-deny | Supply chain security 供应链安全 |
| License compliance 许可证合规 | cargo-deny (licenses)cargo-deny 的 licenses 检查 | Commercial / enterprise projects 商业或企业项目 |
| Supply chain trust 供应链信任校验 | cargo-vetcargo-vet | High-security environments 高安全环境 |
| Find outdated deps 查找过期依赖 | cargo-outdatedcargo-outdated | Scheduled maintenance 周期性维护时 |
| Detect breaking changes 检测破坏性变化 | cargo-semver-checkscargo-semver-checks | Library crate publishing 发布库型 crate 前 |
| Dependency tree analysis 依赖树分析 | cargo tree --duplicatescargo tree --duplicates | Dedup and trim dep graph 去重并精简依赖图 |
| Binary size analysis 二进制体积分析 | cargo-bloatcargo-bloat | Size-constrained deployments 体积敏感的部署环境 |
| Find unused deps 查找未使用依赖 | cargo-udeps / cargo-machetecargo-udeps / cargo-machete | Trim compile time and size 缩短编译时间并减小体积 |
| LTO tuning LTO 调优 | lto = true or "thin"lto = true 或 "thin" | Release binary optimization 发布版二进制优化 |
| Size-optimized binary 体积优先的二进制 | opt-level = "z" + strip = trueopt-level = "z" + strip = true | Embedded / WASM / containers 嵌入式、WASM、容器场景 |
| Unsafe usage audit unsafe 使用审计 | cargo-geigercargo-geiger | Security policy enforcement 执行安全策略 |
| Macro debugging 宏调试 | cargo-expandcargo-expand | Derive / macro_rules debugging 调试 derive 或 macro_rules! |
| Faster linking 更快链接 | mold linkermold 链接器 | Developer inner loop 提升日常迭代效率 |
| Compilation cache 编译缓存 | sccachesccache | CI and local build speed 提升 CI 和本地构建速度 |
| Faster tests 更快跑测试 | cargo-nextestcargo-nextest | CI and local test speed 提升 CI 与本地测试速度 |
| MSRV compliance MSRV 合规 | cargo-msrvcargo-msrv | Library publishing 发布库之前 |
no_std libraryno_std 库 | #![no_std] + default-features = false#![no_std] + default-features = false | Embedded, UEFI, WASM 嵌入式、UEFI、WASM |
| Windows cross-compile Windows 交叉编译 | cargo-xwin / MinGWcargo-xwin / MinGW | Linux → Windows builds 从 Linux 构建 Windows 产物 |
| Platform abstraction 平台抽象 | #[cfg] + trait pattern#[cfg] + trait 模式 | Multi-OS codebases 多操作系统代码库 |
| Windows API calls 调用 Windows API | windows-sys / windows cratewindows-sys / windows crate | Native Windows functionality 原生 Windows 功能开发 |
| End-to-end timing 端到端计时 | hyperfinehyperfine | Whole-binary benchmarks, before/after comparison 整程序基准测试与前后对比 |
| Property-based testing 性质测试 | proptestproptest | Edge case discovery, parser robustness 发现边界条件问题,提升解析器健壮性 |
| Snapshot testing 快照测试 | instainsta | Large structured output verification 验证大块结构化输出 |
| Coverage-guided fuzzing 覆盖率引导模糊测试 | cargo-fuzzcargo-fuzz | Crash discovery in parsers 发现解析器崩溃问题 |
| Concurrency model checking 并发模型检查 | loomloom | Lock-free data structures, atomic ordering 无锁数据结构与原子顺序验证 |
| Feature combination testing feature 组合测试 | cargo-hackcargo-hack | Crates with multiple #[cfg] featuresfeature 分支较多的 crate |
| Fast UB checks (near-native) 快速 UB 检查(接近原生速度) | cargo-carefulcargo-careful | CI safety gate, lighter than Miri CI 安全门禁,成本比 Miri 更低 |
| Auto-rebuild on save 保存即自动重建 | cargo-watchcargo-watch | Developer inner loop, tight feedback 适合日常高频反馈循环 |
| Workspace documentation 工作区文档生成 | cargo doc + rustdoccargo doc + rustdoc | API discovery, onboarding, doc-link CI API 探索、入门引导、文档链接检查 |
| Reproducible builds 可复现构建 | --locked + SOURCE_DATE_EPOCH--locked + SOURCE_DATE_EPOCH | Release integrity verification 验证发布产物完整性 |
| CI cache tuning CI 缓存调优 | Swatinem/rust-cache@v2Swatinem/rust-cache@v2 | Build time reduction (cold → cached) 缩短 CI 构建时间 |
| Workspace lint policy 工作区 lint 策略 | [workspace.lints] in Cargo.tomlCargo.toml 里的 [workspace.lints] | Consistent Clippy/compiler lints across all crates 统一全工作区的 Clippy 与编译器 lint |
| Auto-fix lint warnings 自动修复 lint 警告 | cargo clippy --fixcargo clippy --fix | Automated cleanup of trivial issues 清理简单、机械的警告 |
Further Reading
延伸阅读
| Topic | Resource |
|---|---|
| Cargo build scripts Cargo 构建脚本 | Cargo Book — Build Scripts Cargo Book:Build Scripts |
| Cross-compilation 交叉编译 | Rust Cross-Compilation Rust 交叉编译文档 |
cross toolcross 工具 | cross-rs/cross cross-rs/cross 项目 |
cargo-zigbuildcargo-zigbuild | cargo-zigbuild docs cargo-zigbuild 文档 |
| Criterion.rs Criterion.rs | Criterion User Guide Criterion 使用指南 |
| Divan Divan | Divan docs Divan 文档 |
cargo-llvm-covcargo-llvm-cov | cargo-llvm-cov cargo-llvm-cov 项目 |
cargo-tarpaulincargo-tarpaulin | tarpaulin docs tarpaulin 文档 |
| Miri Miri | Miri GitHub Miri GitHub 项目 |
| Sanitizers in Rust Rust 中的 Sanitizer | rustc Sanitizer docs rustc Sanitizer 文档 |
cargo-makecargo-make | cargo-make book cargo-make 手册 |
cargo-releasecargo-release | cargo-release docs cargo-release 文档 |
cargo-distcargo-dist | cargo-dist docs cargo-dist 文档 |
| Profile-guided optimization 配置文件引导优化 | Rust PGO guide Rust PGO 指南 |
| Flamegraphs 火焰图 | cargo-flamegraph cargo-flamegraph 项目 |
cargo-denycargo-deny | cargo-deny docs cargo-deny 文档 |
cargo-vetcargo-vet | cargo-vet docs cargo-vet 文档 |
cargo-auditcargo-audit | cargo-audit cargo-audit 项目 |
cargo-bloatcargo-bloat | cargo-bloat cargo-bloat 项目 |
cargo-udepscargo-udeps | cargo-udeps cargo-udeps 项目 |
cargo-geigercargo-geiger | cargo-geiger cargo-geiger 项目 |
cargo-semver-checkscargo-semver-checks | cargo-semver-checks cargo-semver-checks 项目 |
cargo-nextestcargo-nextest | nextest docs nextest 文档 |
sccachesccache | sccache sccache 项目 |
mold linkermold 链接器 | mold mold 项目 |
cargo-msrvcargo-msrv | cargo-msrv cargo-msrv 项目 |
| LTO LTO | rustc Codegen Options rustc 代码生成选项文档 |
| Cargo Profiles Cargo Profile | Cargo Book — Profiles Cargo Book:Profiles |
no_stdno_std | Rust Embedded Book Rust Embedded Book |
windows-sys cratewindows-sys crate | windows-rs windows-rs 项目 |
cargo-xwincargo-xwin | cargo-xwin docs cargo-xwin 文档 |
cargo-hackcargo-hack | cargo-hack cargo-hack 项目 |
cargo-carefulcargo-careful | cargo-careful cargo-careful 项目 |
cargo-watchcargo-watch | cargo-watch cargo-watch 项目 |
| Rust CI cache Rust CI 缓存 | Swatinem/rust-cache Swatinem/rust-cache 项目 |
| Rustdoc book Rustdoc 手册 | Rustdoc Book Rustdoc Book |
| Conditional compilation 条件编译 | Rust Reference — cfg Rust Reference:cfg |
| Embedded Rust 嵌入式 Rust | Awesome Embedded Rust Awesome Embedded Rust |
hyperfinehyperfine | hyperfine hyperfine 项目 |
proptestproptest | proptest proptest 项目 |
instainsta | insta snapshot testing insta 快照测试 |
cargo-fuzzcargo-fuzz | cargo-fuzz cargo-fuzz 项目 |
loomloom | loom concurrency testing loom 并发测试 |
Generated as a companion reference — a companion to Rust Patterns and Type-Driven Correctness.
这张卡片作为配套参考资料生成,可与 Rust Patterns 和 Type-Driven Correctness 两本书配合查阅。
Version 1.3 — Added cargo-hack, cargo-careful, cargo-watch, cargo doc, reproducible builds, CI caching strategies, capstone exercise, and chapter dependency diagram for completeness.
版本 1.3:补充了 cargo-hack、cargo-careful、cargo-watch、cargo doc、可复现构建、CI 缓存策略、综合练习与章节依赖图,使内容更完整。