关于 cargo-workflows
Cargo Workflows 是面向处理 Rust 项目的 Agent 技能,涵盖 Cargo 工作区、特性、构建脚本、CI 构建、测试和依赖审计。
主要功能
- 01Cargo 工作区配置
- 02特性标志管理
- 03build.rs 脚本
- 04CI 缓存与增量构建
- 05使用 cargo nextest 测试
- 06依赖审计
适用场景
- 01组织多 crate 的 Rust 项目
- 02为构建选择特性
- 03在 CI 中缓存 Cargo 构建
- 04使用 cargo nextest 运行测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill cargo-workflows安装后在 Claude Code 中输入/cargo-workflows确认已加载。
附带文件references/workspace-patterns.md
详细介绍
Cargo 工作流
用途
指导 Agent 使用 Cargo 工作区、管理特性、编写构建脚本(build.rs)、集成 CI、进行增量编译,以及使用 Cargo 工具生态。
触发条件
- “如何设置包含多个 crate 的 Cargo 工作区?”
- “Cargo 中的特性如何工作?”
- “如何编写
build.rs脚本?” - “如何加快 CI 中的 Cargo 构建?”
- “如何审计 Rust 依赖项?”
- “什么是 cargo nextest?我应该使用它吗?”
工作流
1. 设置工作区
my-project/
├── Cargo.toml # Workspace root
├── Cargo.lock # Single lock file for all members
├── crates/
│ ├── core/
│ │ └── Cargo.toml
│ ├── cli/
│ │ └── Cargo.toml
│ └── server/
│ └── Cargo.toml
└── tools/
└── codegen/
└── Cargo.toml
# Workspace root Cargo.toml
[workspace]
members = [
"crates/core",
"crates/cli",
"crates/server",
"tools/codegen",
]
resolver = "2" # Feature resolver v2 (required for edition 2021)
# Shared dependency versions (workspace.dependencies)
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
anyhow = "1"
# Shared profile settings
[profile.release]
lto = "thin"
codegen-units = 1
# Member Cargo.toml
[package]
name = "myapp-core"
version.workspace = true
edition.workspace = true
[dependencies]
serde.workspace = true # Inherit from workspace
anyhow.workspace = true
2. 特性标志
[features]
default = ["std"]
# Simple flag
std = []
# Feature that enables another feature
full = ["std", "async", "serde-support"]
# Feature with optional dependency
async = ["dep:tokio"]
serde-support = ["dep:serde", "serde/derive"]
[dependencies]
tokio = { version = "1", optional = true }
serde = { version = "1", optional = true }
# Build with specific features
cargo build --features "async,serde-support"
# Build with no default features
cargo build --no-default-features
# Build with all features
cargo build --all-features
# Check feature combinations
cargo check --no-default-features
cargo check --all-features
特性的注意事项:
- 特性具有累加性:一旦在依赖关系图中的任何位置启用,就会一直保持启用
resolver = "2"可防止开发依赖项与常规依赖项之间的特性泄漏- 使用
dep:optional_dep语法(edition 2021),避免隐式创建特性
3. 构建脚本(build.rs)
// build.rs (at crate root, runs before compilation)
use std::env;
use std::path::PathBuf;
fn main() {
// Re-run if these files change
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-env-changed=MY_LIB_PATH");
// Link a system library
println!("cargo:rustc-link-lib=mylib");
println!("cargo:rustc-link-search=/usr/local/lib");
// Pass a cfg flag to Rust code
let target = env::var("TARGET").unwrap();
if target.contains("linux") {
println!("cargo:rustc-cfg=target_os_linux");
}
// Set environment variable for downstream crates
println!("cargo:rustc-env=MY_GENERATED_VAR=value");
// Generate bindings with bindgen
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("bindings.rs")).unwrap();
}
println! 指令 | 效果 |
|---|---|
cargo:rerun-if-changed=FILE | 文件变更时重新运行构建脚本 |
cargo:rerun-if-env-changed=VAR | 环境变量变更时重新运行 |
cargo:rustc-link-lib=NAME | 链接库 |
cargo:rustc-link-search=PATH | 添加库搜索路径 |
cargo:rustc-cfg=FLAG | 在代码中启用 #[cfg(FLAG)] |
cargo:rustc-env=KEY=VAL | 在编译时设置 env!("KEY") |
cargo:warning=MSG | 发出构建警告 |
4. 增量构建与 CI 缓存
# GitHub Actions with sccache
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
shared-key: "release-build"
# Or manual cache
- uses: actions/cache@v3
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
# Warm cache locally
cargo fetch # Download all deps without building
cargo build --tests # Build everything including test bins
# Check if incremental hurts release builds (it often does)
[profile.release]
incremental = false # Default; leave false for release
5. cargo nextest(更快的测试运行器)
# Install
cargo install cargo-nextest
# Run tests (parallel by default, better output)
cargo nextest run
# Run with specific filter
cargo nextest run test_name_pattern
# List tests without running
cargo nextest list
# Use in CI (JUnit output)
cargo nextest run --profile ci
nextest.toml:
[profile.ci]
fail-fast = false
test-threads = "num-cpus"
retries = { backoff = "exponential", count = 2, delay = "1s" }
[profile.default]
test-threads = "num-cpus"
6. 依赖项管理与审计
# Check for security advisories
cargo install cargo-audit
cargo audit
# Deny specific licenses, duplicates, advisories
cargo install cargo-deny
cargo deny check
# Check for unused dependencies
cargo install cargo-machete
cargo machete
# Update dependencies
cargo update # Update to compatible versions
cargo update -p serde # Update single package
cargo upgrade # Update to latest (cargo-edit)
deny.toml:
[licenses]
allow = ["MIT", "Apache-2.0", "BSD-2-Clause", "BSD-3-Clause"]
deny = ["GPL-2.0", "AGPL-3.0"]
[bans]
multiple-versions = "warn"
deny = [{ name = "openssl", reason = "Use rustls instead" }]
[advisories]
ignore = [] # List advisory IDs to ignore
7. 实用的 cargo 命令
# Build only specific binary
cargo build --bin myapp
# Build only specific example
cargo build --example myexample
# Run with arguments
cargo run -- --flag arg1 arg2
# Expand macros (for debugging proc macros)
cargo install cargo-expand
cargo expand module::path
# Tree of dependencies
cargo tree
cargo tree --duplicates # Show crates with multiple versions
cargo tree -i serde # Who depends on serde?
# Cargo.toml metadata
cargo metadata --format-version 1 | jq '.packages[].name'
有关工作区模式和依赖项解析的详情,请参阅 references/workspace-patterns.md。
相关技能
- 使用
skills/rust/rustc-basics配置编译器标志和配置文件 - 使用
skills/rust/rust-debugging调试通过 Cargo 构建的二进制文件 - 使用
skills/rust/rust-ffi为 C 库绑定编写build.rs - 将 Rust 集成到 CMake 构建中时,使用
skills/build-systems/cmake
评论
支持 Markdown