What it is
Cargo is the build system and package manager for the Rust programming language, used for compiling Rust code, managing dependencies, and running tests.
Installation
Cargo is installed automatically with Rustup.
To install Rust and Cargo:
curl --proto '=https' --tlsv1.2 -sSF https://sh.rustup.rs | sh
source $HOME/.cargo/env
Core Concepts
- Crates: A Rust crate is a compilation unit. It can be a library or an executable. Cargo manages dependencies between crates.
- Dependencies: External crates that your project relies on. Cargo fetches and compiles them automatically.
- Manifest (
Cargo.toml): The configuration file for a Cargo project. It defines package metadata, dependencies, build profiles, and more. - Build Profiles: Predefined sets of compiler flags and optimizations. Common profiles are
dev(for development, fast compilation) andrelease(for production, optimized for speed and size). - Workspaces: A feature in Cargo that allows managing multiple related crates within a single project, sharing a single
Cargo.lockfile and build artifacts.
Commands / Usage
Project Management
cargo new <project_name>Create a new binary (executable) project.cargo new my_rust_appcargo new --lib <project_name>Create a new library project.cargo new --lib my_rust_libcargo initInitialize a new Cargo project in the current directory.cargo initcargo init --libInitialize a new Cargo library project in the current directory.cargo init --libcargo checkCheck a package for errors without compiling it. Faster thancargo build.cargo checkcargo buildCompile a package and its dependencies. Default is debug build.cargo buildcargo build --releaseCompile a package with optimizations for release.cargo build --releasecargo build --target <target_triple>Compile for a specific target architecture.cargo build --target x86_64-unknown-linux-gnucargo cleanRemove thetargetdirectory, cleaning the build artifacts.cargo clean
Running and Testing
cargo runCompile and run a binary crate.cargo runcargo run --releaseCompile and run a binary crate with optimizations.cargo run --releasecargo run --bin <binary_name>Compile and run a specific binary from a project with multiple binaries.cargo run --bin my_other_appcargo testRun all tests in a package.cargo testcargo test --releaseRun all tests with optimizations.cargo test --releasecargo test <test_name>Run a specific test function by name.cargo test test_additioncargo test --docRun documentation tests.cargo test --doccargo benchRun benchmarks in a package.cargo bench
Dependency Management
cargo build(when dependencies are missing) Automatically fetches and compiles new dependencies listed inCargo.toml.# Add serde = "1.0" to [dependencies] in Cargo.toml cargo buildcargo updateUpdate all dependencies to the latest compatible versions according toCargo.tomland regenerateCargo.lock.cargo updatecargo update --package <crate_name>Update a specific dependency.cargo update --package serdecargo add <crate_name>Add a new dependency toCargo.toml.cargo add randcargo add <crate_name> --git <repo_url>Add a dependency from a Git repository.cargo add crossbeam --git https://github.com/crossbeam-rs/crossbeamcargo add <crate_name> --path <local_path>Add a local dependency.cargo add my_local_lib --path ../my_local_libcargo rm <crate_name>Remove a dependency fromCargo.toml.cargo rm rand
Publishing and Packaging
cargo publishUpload a package to crates.io. Requires authentication.cargo publishcargo docBuild documentation for the current crate and all its dependencies.cargo doccargo doc --openBuild documentation and open it in the default web browser.cargo doc --opencargo packageCreate a local copy of the current package intarget/package.cargo package
Workspaces
cargo new --workspace <path>Initialize a new workspace.cargo new --workspace my_workspace cd my_workspace cargo new --lib crate_a cargo new --lib crate_bcargo build(in workspace root) Builds all crates in the workspace.# From the my_workspace directory cargo buildcargo test(in workspace root) Runs tests for all crates in the workspace.# From the my_workspace directory cargo test
Other Useful Commands
cargo install <crate_name>Install a binary crate from crates.io.cargo install ripgrepcargo install --git <repo_url>Install a binary crate from a Git repository.cargo install --git https://github.com/BurntSushi/ripgrepcargo search <query>Search for crates on crates.io.cargo search serdecargo metadataOutput the project’s metadata in JSON format.cargo metadatacargo treeDisplay the dependency graph.cargo treecargo tree --depth 1Display direct dependencies only.cargo tree --depth 1
Common Patterns
- Building for production:
cargo build --release - Running a release build:
cargo run --release - Checking code quickly:
cargo check - Running tests and checking code:
cargo test && cargo check - Adding a dependency and rebuilding:
cargo add serde cargo build - Updating all dependencies:
cargo update cargo build - Cleaning and rebuilding from scratch:
cargo clean && cargo build - Building for a specific platform (e.g., Raspberry Pi):
First, install the target:
Then build:rustup target add armv7-unknown-linux-gnueabihfcargo build --target armv7-unknown-linux-gnueabihf - Generating documentation and opening it:
cargo doc --open - Finding which crate provides a specific function (using
cargo search):cargo search "for:function_name" - Viewing the dependency tree:
cargo tree
Gotchas
Cargo.lockfile: This file pins exact versions of your dependencies. It should be committed to version control to ensure reproducible builds. Do not edit it manually.- Build artifacts: By default,
cargo buildcreates debug builds intarget/debug/.cargo build --releasecreates optimized builds intarget/release/. - Dependency resolution: Cargo’s dependency resolver can sometimes struggle with complex dependency graphs.
cargo updatecan help, but occasionally you might need to adjust versions inCargo.tomlmanually. cargo installpath: Binaries installed withcargo installare typically placed in$HOME/.cargo/bin/. Ensure this directory is in your system’sPATHto run them directly.--targetvs.--triple: The--targetflag is used for cross-compilation. The value is a target triple (e.g.,x86_64-unknown-linux-gnu).cargo publishauthentication: You need an API token from crates.io. Obtain it usingcargo login.- Workspaces and
Cargo.lock: In a workspace, there is a singleCargo.lockfile at the workspace root, not in each individual crate directory. This ensures all crates in the workspace use compatible dependency versions.