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]

PARAMETERS

--target
    Build for the specified target architecture. If not specified, defaults to the host platform.

--release
    Build in release mode, with optimizations enabled.

--profile
    Build with the specified profile. Overrides the default debug/release behavior.

--features
    Enable or disable features.

--all-features
    Enable all features.

--no-default-features
    Disable default features.

--target-dir
    Directory for all generated artifacts.

--message-format
    Format of build messages (json, human).

-v, --verbose
    Use verbose output.

-q, --quiet
    Do not print cargo log messages.

--color
    Control coloring: auto, always, never.

--frozen
    Require Cargo.lock and .cargo/config are up to date.

--locked
    Require Cargo.lock is up to date.

--offline
    Run without accessing the network.

DESCRIPTION

The `cargo build` command compiles the current package or workspace and its dependencies. By default, it compiles in debug mode, suitable for development. You can use the `--release` flag to compile in release mode, which optimizes the code for performance and is intended for deployment. Cargo automatically manages dependencies, downloads required crates, and links them correctly. The build process involves compiling source code, linking object files, and generating executables or libraries. The output location defaults to the `target` directory in your project's root. Building with different profiles (debug/release) can significantly impact the generated code's size and speed. Cargo handles incremental compilation to avoid unnecessary rebuilds by caching previously compiled crates. You can force a full rebuild by running `cargo clean` before building.

EXIT STATUS

Cargo returns 0 on success. Any other value indicates failure. Common reasons for failure include compilation errors, dependency resolution problems, or issues with the Cargo.toml file.

ENVIRONMENT VARIABLES

Cargo uses environment variables to configure the build process. Key variables include `RUSTFLAGS` for passing flags to the Rust compiler, and `CARGO_TARGET_DIR` for overriding the default target directory. Proxy settings are read from standard environment variables like `http_proxy` and `https_proxy`.

SEE ALSO

cargo run(1), cargo test(1), cargo clean(1), cargo check(1)

Copied to clipboard