$linuxjunkies
>

cargo(1)

Cargo is Rust's package manager and build system for creating, testing, and distributing Rust projects.

UbuntuDebianFedoraArch

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

FlagWhat it does
buildcompile the current package
runrun a binary from the current package
testrun tests for the current package
checkcheck code without generating artifacts (faster than build)
addadd dependency to Cargo.toml
newcreate a new Rust package
--releasebuild with optimizations enabled
--featuresspace-separated list of features to activate
-jset number of parallel jobs; defaults to CPU count
publishupload package to crates.io
--verboseprint verbose output
docgenerate and open documentation

Examples

create a new binary project named 'my_project' with basic structure

cargo new my_project

compile the project with optimizations, binary in target/release/

cargo build --release

build and run the binary, passing arg1 and arg2 as arguments

cargo run -- arg1 arg2

run only library unit tests (skip integration tests)

cargo test --lib

add the serde crate as a dependency to Cargo.toml

cargo add serde

quickly verify code compiles without building artifacts

cargo check

generate HTML documentation and open in browser

cargo doc --open

build with the 'experimental' feature flag enabled

cargo build --features experimental

Related commands