LinuxCommandLibrary

cargo-build

Build Rust projects

TLDR

Build the package or packages defined by the Cargo.toml manifest file in the local path

$ cargo [[b|build]]
copy

Build artifacts in release mode, with optimizations
$ cargo [[b|build]] [[-r|--release]]
copy

Require that Cargo.lock is up to date
$ cargo [[b|build]] --locked
copy

Build all packages in the workspace
$ cargo [[b|build]] --workspace
copy

Build a specific package
$ cargo [[b|build]] [[-p|--package]] [package]
copy

Build only the specified binary
$ cargo [[b|build]] --bin [name]
copy

Build only the specified test target
$ cargo [[b|build]] --test [test_name]
copy

SYNOPSIS

cargo build [options…] [--] [<paths…>]

PARAMETERS

--release
    Build optimized release artifacts (default: debug).
Output in target/release.

--bin <NAME>…
    Build only the specified binary.

--example <NAME>…
    Build only the specified example.

--test <NAME>…
    Build only the specified integration test.

--bench <NAME>…
    Build only the specified benchmark.

--lib
    Build library only, skipping binaries.

-p <SPEC>, --package <SPEC>…
    Build specific package(s) (see cargo pkgid)

-w, --workspace
    Build all packages in workspace.

-j <N>, --jobs <N>
    Number of parallel jobs (default: CPU cores).

--target <TRIPLE>
    Build for specific target triple (requires toolchain).

--target-dir <DIR>
    Custom directory for all artifacts (default: target).

--manifest-path <PATH>
    Path to Cargo.toml.

-v…, --verbose…
    Verbose output (-vv for very verbose).

-q, --quiet
    Suppress cargo log messages.

--color <WHEN>
    Color output: auto, always, never.

--frozen
    Require Cargo.lock and cache unchanged.

--locked
    Require Cargo.lock up to date.

DESCRIPTION

cargo build is the primary build command in Cargo, Rust's package manager and build system. It compiles a Rust project defined in Cargo.toml, producing executables for binaries, dynamic/static libraries, or other artifacts as configured. By default, it creates unoptimized debug builds optimized for fast compilation and debugging, placed in the target/debug directory.

Cargo handles the entire dependency resolution process, fetching crates from crates.io or Git repositories, compiling them incrementally, and linking everything together. Builds are reproducible and support workspaces for multi-crate projects.

Use --release for production-ready optimized binaries in target/release, which disable debugging but enable performance optimizations like inlining and loop unrolling via the Rust compiler rustc.

The command supports selective builds (specific binaries, examples, tests), cross-compilation to other platforms with --target, and parallel compilation controlled by --jobs. Incremental compilation ensures only changed code is rebuilt, making iterative development efficient. Verbose output via -v aids debugging build issues.

CAVEATS

First builds download/compile dependencies and can be slow. Requires Rust toolchain (install via rustup). Release builds take longer initially. Cross-compilation needs target toolchain installed.

OUTPUT LOCATIONS

Debug: target/debug/ (executables lack extension on Unix).
Release: target/release/.
Libraries: target/debug/libNAME.{a,so}.

INCREMENTAL BUILDS

Enabled by default; cleans with cargo clean. Speeds up after first run.

HISTORY

Cargo debuted in 2014 with Rust 0.9 as Mozilla's build tool. cargo build core since launch, gaining incremental builds (2015), workspaces (Rust 1.19, 2017), better parallelism, and cross-compile support. Now Rust Foundation project.

SEE ALSO

cargo(1), rustc(1), rustdoc(1), rustup(1)

Copied to clipboard