Rust Cargo Cheat Sheet
Reference for Cargo commands, Cargo.toml structure, dependency management, workspaces, and common cargo subcommands for building and testing Rust projects.
2 PagesBeginnerApr 12, 2026
Basic Commands
The core cargo workflow for building and running projects.
bash
cargo new my_project # Create a new binary projectcargo new --lib my_lib # Create a new library projectcargo build # Compile in debug mode (target/debug)cargo build --release # Compile with optimizations (target/release)cargo run # Build and run the binarycargo run -- arg1 arg2 # Pass arguments to the binarycargo check # Fast type-check without producing a binarycargo test # Run all testscargo test test_name # Run tests matching a name filter
Cargo.toml
The project manifest that declares metadata and dependencies.
yaml
[package]name = "my_project"version = "0.1.0"edition = "2021"[dependencies]serde = { version = "1.0", features = ["derive"] }tokio = { version = "1", features = ["full"] }rand = "0.8"[dev-dependencies]criterion = "0.5"[profile.release]opt-level = 3lto = true
Common Subcommands
Frequently used cargo tooling commands.
- cargo fmt- Formats source code according to rustfmt rules
- cargo clippy- Runs the Clippy linter for idiomatic-code suggestions and common mistakes
- cargo doc --open- Builds and opens crate documentation in a browser
- cargo add <crate>- Adds a dependency to Cargo.toml (Cargo 1.62+)
- cargo update- Updates dependencies in Cargo.lock to the latest compatible versions
- cargo publish- Publishes the crate to crates.io
- cargo tree- Prints the dependency tree, useful for debugging version conflicts
- cargo clean- Removes the target directory build artifacts
Workspaces
Managing multiple related crates in a single repository.
toml
# Top-level Cargo.toml for a multi-crate workspace[workspace]members = [ "app", "core", "cli",]resolver = "2"[workspace.dependencies]serde = { version = "1.0", features = ["derive"] }# cargo build --workspace # Build every crate in the workspace# cargo test -p core # Run tests only for the "core" package
Dependency Version Specs
Syntax for constraining dependency versions in Cargo.toml.
- "1.2.3"- Caret requirement (default); allows updates that don't change the leftmost non-zero digit
- "=1.2.3"- Exact version requirement, no automatic updates
- "~1.2"- Tilde requirement; allows patch-level updates only (1.2.x)
- git = "url"- Pulls a dependency directly from a Git repository
- path = "../local-crate"- Uses a local path dependency, common in workspaces
- features = [...]- Enables optional, opt-in functionality exposed by the dependency
- default-features = false- Disables a dependency's default feature set to reduce compile time/size
Pro Tip
Commit Cargo.lock for binary applications (reproducible builds) but omit it for libraries, so downstream consumers resolve dependency versions against their own lockfile.
Was this cheat sheet helpful?
Explore Topics
#RustCargo#RustCargoCheatSheet#Programming#Beginner#BasicCommands#CargoToml#CommonSubcommands#Workspaces#Testing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance