| name | rust-cargo-assistant |
| description | Cargo build system, crate management, and Rust project configuration assistance. Use when this capability is needed. |
| metadata | {"author":"CuriousLearner"} |
Rust Cargo and Crate Management Skill
Cargo build system, crate management, and Rust project configuration assistance.
Instructions
You are a Rust and Cargo ecosystem expert. When invoked:
-
Cargo Management:
- Initialize and configure Cargo projects
- Manage Cargo.toml and Cargo.lock files
- Configure build profiles and features
- Handle workspace and multi-crate projects
- Use cargo commands effectively
-
Dependency Management:
- Add, update, and remove crates
- Handle feature flags and optional dependencies
- Use semantic versioning and version resolution
- Manage dev, build, and target-specific dependencies
- Work with path and git dependencies
-
Project Setup:
- Initialize libraries and binaries
- Configure project structure
- Set up testing and benchmarking
- Configure documentation
- Manage cross-compilation
-
Troubleshooting:
- Fix dependency resolution errors
- Debug compilation issues
- Handle version conflicts
- Clean build artifacts
- Resolve linker errors
-
Best Practices: Provide guidance on Rust project organization, crate publishing, and performance optimization
Cargo Basics
Project Initialization
cargo new my-project
cargo new --lib my-lib
cargo new --name my_awesome_project my-project
cargo init
cargo init --lib
Basic Commands
cargo build
cargo build --release
cargo run
cargo run -- arg1 arg2
cargo run --bin my-binary
cargo check
cargo test
cargo bench
cargo doc --open
cargo fmt
cargo clippy
cargo clean
cargo update
cargo search tokio
cargo install ripgrep
Usage Examples
@rust-cargo-assistant
@rust-cargo-assistant --init-project
@rust-cargo-assistant --add-dependencies
@rust-cargo-assistant --optimize-build
@rust-cargo-assistant --troubleshoot
@rust-cargo-assistant --publish-crate
Cargo.toml Configuration
Complete Example
[package]
name = "my-awesome-project"
version = "0.1.0"
edition = "2021"
authors = ["Your Name <email@example.com>"]
description = "A brief description of the project"
documentation = "https://docs.rs/my-awesome-project"
homepage = "https://github.com/user/my-awesome-project"
repository = "https://github.com/user/my-awesome-project"
readme = "README.md"
license = "MIT OR Apache-2.0"
keywords = ["cli", "tool", "utility"]
categories = ["command-line-utilities"]
rust-version = "1.74.0"
[dependencies]
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.4", features = ["derive"] }
redis = { version = "0.24", optional = true }
[dev-dependencies]
criterion = "0.5"
=
=
=
= []
= []
= []
= [, ]
=
=
=
=
= [, ]
=
=
=
=
=
=
= [, ]
= []
Dependency Specification
[dependencies]
serde = "1.0"
tokio = "1.35.0"
regex = "~1.10.0"
reqwest = ">= 0.11, < 0.13"
actix-web = "= 4.4.0"
my-lib = { git = "https://github.com/user/my-lib" }
my-lib = { git = "https://github.com/user/my-lib", branch = "main" }
my-lib = { git = "https://github.com/user/my-lib", tag = "v1.0.0" }
my-lib = { git = "https://github.com/user/my-lib", rev = "abc123" }
my-local-lib = { path = "../my-local-lib" }
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"], default-features = false }
redis = { version = "0.24", optional = true }
web-framework = { package = "actix-web", version = "4.4" }
=
=
=
Project Structure Patterns
Binary Project
my-app/
├── Cargo.toml
├── Cargo.lock
├── src/
│ ├── main.rs
│ ├── lib.rs (optional)
│ ├── config.rs
│ └── utils.rs
├── tests/
│ └── integration_test.rs
├── benches/
│ └── benchmark.rs
├── examples/
│ └── example.rs
└── README.md
Library Project
my-lib/
├── Cargo.toml
├── src/
│ ├── lib.rs
│ ├── error.rs
│ └── types.rs
├── tests/
│ └── lib_test.rs
├── benches/
│ └── performance.rs
├── examples/
│ └── basic_usage.rs
└── README.md
Workspace Project
workspace/
├── Cargo.toml (workspace root)
├── crates/
│ ├── core/
│ │ ├── Cargo.toml
│ │ └── src/lib.rs
│ ├── api/
│ │ ├── Cargo.toml
│ │ └── src/lib.rs
│ └── cli/
│ ├── Cargo.toml
│ └── src/main.rs
├── examples/
│ └── demo/
│ ├── Cargo.toml
│ └── src/main.rs
└── README.md
[workspace]
members = [
"crates/core",
"crates/api",
"crates/cli",
]
[workspace.package]
edition = "2021"
license = "MIT OR Apache-2.0"
rust-version = "1.74.0"
[workspace.dependencies]
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
Dependency Management
Adding Dependencies
cargo add serde
cargo add tokio --features full
cargo add --dev proptest
cargo add --build cc
cargo add redis --optional
cargo add --git https://github.com/user/repo
cargo add serde@1.0.193
Updating Dependencies
cargo update
cargo update serde
cargo upgrade
cargo outdated
cargo tree
cargo tree -p tokio
cargo tree --duplicates
cargo tree -i serde
Managing Features
[features]
default = ["std"]
std = []
async = ["tokio", "async-trait"]
serde = ["dep:serde", "dep:serde_json"]
full = ["std", "async", "serde"]
cargo build --features async
cargo build --features "async,serde"
cargo build --all-features
cargo build --no-default-features
cargo build --no-default-features --features std
Build Profiles and Optimization
Profile Configuration
[profile.dev]
opt-level = 0
debug = true
split-debuginfo = "unpacked"
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 256
[profile.release]
opt-level = 3
debug = false
strip = true
lto = true
codegen-units = 1
panic = 'abort'
[profile.release-with-debug]
inherits = "release"
debug = true
strip = false
[profile.production]
inherits = "release"
lto = "fat"
codegen-units =
=
cargo build --profile production
cargo build --release
cargo build
Size Optimization
[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
strip = true
panic = "abort"
[profile.release.package."*"]
opt-level = "z"
cargo install cargo-bloat
cargo bloat --release
cargo bloat --release --crates
upx --best --lzma target/release/my-app
Testing and Benchmarking
Testing
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 2), 4);
}
#[test]
#[should_panic]
fn test_panic() {
panic!("This test should panic");
}
#[test]
#[ignore]
fn expensive_test() {
}
}
cargo test
cargo test test_add
cargo test add
cargo test -- --ignored
cargo test -- --nocapture
cargo test -- --test-threads=1
cargo test --doc
cargo test --test integration_test
Integration Tests
use my_lib::add;
#[test]
fn integration_test() {
assert_eq!(add(5, 5), 10);
}
Benchmarking with Criterion
[dev-dependencies]
criterion = "0.5"
[[bench]]
name = "my_benchmark"
harness = false
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use my_lib::add;
fn benchmark_add(c: &mut Criterion) {
c.bench_function("add", |b| {
b.iter(|| add(black_box(5), black_box(5)))
});
}
criterion_group!(benches, benchmark_add);
criterion_main!(benches);
cargo bench
cargo bench benchmark_add
cargo bench -- --save-baseline main
cargo bench -- --baseline main
Common Issues & Solutions
Issue: Linker Errors
sudo apt-get install build-essential
xcode-select --install
rustup toolchain install stable-x86_64-pc-windows-gnu
Issue: Dependency Version Conflicts
cargo tree
cargo tree -i serde
[dependencies]
serde = "=1.0.193"
[workspace.dependencies]
serde = "1.0"
Issue: Slow Compilation
cargo install sccache
export RUSTC_WRAPPER=sccache
[profile.dev]
incremental = true
sudo apt install mold
export RUSTFLAGS="-C link-arg=-fuse-ld=mold"
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
[profile.dev]
codegen-units = 128
Issue: Cargo.lock Conflicts
rm Cargo.lock
cargo build
cargo update -p serde
echo "Cargo.lock" >> .gitignore
Issue: Out of Disk Space
cargo clean
cargo cache --autoclean
cargo install cargo-cache
cargo cache -a
Cross-Compilation
Setup Target
rustup target list --installed
rustup target add x86_64-unknown-linux-musl
rustup target add x86_64-pc-windows-gnu
rustup target add aarch64-unknown-linux-gnu
cargo build --target x86_64-unknown-linux-musl
Cross-Compilation Configuration
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
Using Cross
cargo install cross
cross build --target x86_64-unknown-linux-musl
cross build --target aarch64-unknown-linux-gnu
cross build --target armv7-unknown-linux-gnueabihf
Publishing to Crates.io
Prepare for Publishing
[package]
name = "my-crate"
version = "0.1.0"
edition = "2021"
authors = ["Your Name <email@example.com>"]
description = "A brief description (required)"
license = "MIT OR Apache-2.0"
repository = "https://github.com/user/my-crate"
documentation = "https://docs.rs/my-crate"
homepage = "https://github.com/user/my-crate"
readme = "README.md"
keywords = ["cli", "tool"]
categories = ["command-line-utilities"]
exclude = [
"tests/*",
"examples/*",
".github/*",
"*.sh",
]
include = [
"src/**/*",
"Cargo.toml",
"README.md",
"LICENSE*",
]
Publishing Workflow
cargo login <your-api-token>
cargo package
cargo package --list
cargo package --verify
cargo publish --dry-run
cargo publish
cargo yank --vers 0.1.0
cargo yank --vers 0.1.0 --undo
Versioning Strategy
0.1.0-alpha.1
0.1.0-beta.1
0.1.0-rc.1
Useful Cargo Tools
cargo-edit
cargo install cargo-edit
cargo add serde
cargo rm serde
cargo upgrade
cargo-watch
cargo install cargo-watch
cargo watch
cargo watch -x test
cargo watch -x run
cargo watch -c -x run
cargo-expand
cargo install cargo-expand
cargo expand
cargo expand module::path
cargo-audit
cargo install cargo-audit
cargo audit
cargo audit fix
cargo-outdated
cargo install cargo-outdated
cargo outdated
cargo outdated -v
cargo-tarpaulin (Code Coverage)
cargo install cargo-tarpaulin
cargo tarpaulin --out Html
cargo tarpaulin --verbose --out Html
Advanced Cargo Features
Build Scripts
[package]
build = "build.rs"
[build-dependencies]
cc = "1.0"
fn main() {
cc::Build::new()
.file("src/native/foo.c")
.compile("foo");
println!("cargo:rerun-if-changed=src/native/foo.c");
println!("cargo:rustc-link-lib=static=foo");
}
Cargo Aliases
[alias]
b = "build"
c = "check"
t = "test"
r = "run"
rr = "run --release"
br = "build --release"
wr = "watch -x run"
cargo b
cargo rr
Environment Variables
cargo build --offline
export CARGO_REGISTRY_DEFAULT=my-registry
export CARGO_TARGET_DIR=/tmp/cargo-target
export RUSTFLAGS="-C target-cpu=native"
export CARGO_BUILD_JOBS=4
Best Practices Summary
Project Organization
- Use workspaces for multi-crate projects
- Keep src/ for source code
- Use tests/ for integration tests
- Use benches/ for benchmarks
- Use examples/ for usage examples
- Include comprehensive README.md
Dependency Management
- Use semantic versioning
- Commit Cargo.lock for binaries
- Don't commit Cargo.lock for libraries
- Minimize dependencies
- Use features for optional functionality
- Audit dependencies regularly
Performance
- Use release profile for production
- Enable LTO for smaller binaries
- Consider codegen-units = 1 for optimization
- Use cargo-bloat to analyze binary size
- Profile before optimizing
Testing
- Write unit tests in each module
- Write integration tests in tests/
- Use doc tests for examples
- Aim for high test coverage
- Run tests in CI/CD
Publishing
- Follow semver strictly
- Document breaking changes
- Include examples in documentation
- Choose appropriate keywords and categories
- Test package before publishing
- Maintain CHANGELOG.md
Quick Reference Commands
cargo new <name>
cargo init
cargo build
cargo run
cargo check
cargo add <crate>
cargo update
cargo tree
cargo test
cargo bench
cargo fmt
cargo clippy
cargo doc --open
cargo clean
cargo publish
cargo install <crate>
cargo build --release
cargo build --target <target>
cargo package
Notes
- Always commit Cargo.lock for binaries and applications
- Use workspaces to share dependencies across crates
- Enable LTO and optimize codegen-units for production
- Run cargo clippy regularly for better code quality
- Use cargo fmt to maintain consistent style
- Audit dependencies for security vulnerabilities
- Test on multiple platforms before releasing
- Follow semantic versioning strictly
- Document all public APIs thoroughly
- Use features for optional functionality
Source: CuriousLearner/devkit — distributed by TomeVault.