cargo(1)
Cargo is Rust's package manager and build system for creating, testing, and distributing Rust projects.
Synopsis
cargo [OPTIONS] [COMMAND]Description
Cargo manages Rust project dependencies, compiles source code into binaries or libraries, runs tests, and publishes packages to crates.io. Every Rust project has a Cargo.toml manifest file defining metadata, dependencies, and build configuration.
Cargo automates the entire development workflow: fetching dependencies, handling version resolution, building with appropriate compiler flags, and running tests. It supports workspace projects with multiple interdependent crates and provides hooks for custom build scripts.
Common options
| Flag | What it does |
|---|---|
build | compile the current package |
run | run a binary from the current package |
test | run tests for the current package |
check | check code without generating artifacts (faster than build) |
add | add dependency to Cargo.toml |
new | create a new Rust package |
--release | build with optimizations enabled |
--features | space-separated list of features to activate |
-j | set number of parallel jobs; defaults to CPU count |
publish | upload package to crates.io |
--verbose | print verbose output |
doc | generate and open documentation |
Examples
create a new binary project named 'my_project' with basic structure
cargo new my_projectcompile the project with optimizations, binary in target/release/
cargo build --releasebuild and run the binary, passing arg1 and arg2 as arguments
cargo run -- arg1 arg2run only library unit tests (skip integration tests)
cargo test --libadd the serde crate as a dependency to Cargo.toml
cargo add serdequickly verify code compiles without building artifacts
cargo checkgenerate HTML documentation and open in browser
cargo doc --openbuild with the 'experimental' feature flag enabled
cargo build --features experimental