Cargo (Rust)

Cargo is Rust's official build tool and package manager. It compiles your code, downloads and tracks dependencies, runs tests, and builds release binaries — all from simple commands.

Rust

Cargo is to Rust what npm is to Node or pip is to Python, but it also handles building. cargo new starts a project, cargo build compiles it, cargo run builds and runs, cargo test runs tests, and cargo build --release produces an optimized binary.

Your project's configuration lives in Cargo.toml: package metadata and a [dependencies] section listing crates (packages) and version requirements. Cargo.lock pins exact resolved versions for reproducible builds and should be committed for applications.

Cargo fetches dependencies from crates.io, Rust's central package registry. It manages the whole workflow, so most Rust developers rarely invoke rustc (the compiler) directly.

Syntax

cargo new myapp
cargo build
cargo run
cargo test

Examples

Rust — Cargo.toml declares the package and its dependencies.

# Cargo.toml
[package]
name = "myapp"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "1.0"

Learn it in a lesson

See also

Back to the Dictionary