LinuxCommandLibrary

cargo

Manage Rust projects

TLDR

Search for crates

$ cargo search [search_string]
copy

Install a binary crate
$ cargo install [crate_name]
copy

List installed binary crates
$ cargo install --list
copy

Create a new binary or library Rust project in the specified directory (or the current working directory by default)
$ cargo init --[bin|lib] [path/to/directory]
copy

Add a dependency to Cargo.toml in the current directory
$ cargo add [dependency]
copy

Build the Rust project in the current directory using the release profile
$ cargo [[b|build]] [[-r|--release]]
copy

Build the Rust project in the current directory using the nightly compiler (requires rustup)
$ cargo +nightly [[b|build]]
copy

Build using a specific number of threads (default is the number of logical CPUs)
$ cargo [[b|build]] --jobs [number_of_threads]
copy

SYNOPSIS

cargo COMMAND [OPTIONS] [ARGS...]
or
cargo [OPTIONS]

PARAMETERS

--version
    Display cargo version information.

--help
    Display general help message or command-specific help.

--list
    List all installed subcommands.

-v, --verbose
    Use verbose output, showing more details like dependency information.

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

--color
    Control coloring of output. WHEN can be 'auto', 'always', or 'never'.

build
    Compile the current package and all of its dependencies.

run
    Compile and execute the current package.

test
    Run the tests for the current package.

check
    Analyze the current package and report errors, but do not build.

new
    Create a new Rust project (binary or library) at the specified path.

init
    Create a new Rust project in an existing directory.

add
    Add a dependency to Cargo.toml for the current package.

clean
    Remove generated artifacts and the target directory.

publish
    Upload the current package to the crates.io registry.

install
    Install a Rust binary (executable) from a given crate.

DESCRIPTION

cargo is the official package manager and build system for the Rust programming language. It is an indispensable tool for Rust developers, streamlining the entire development lifecycle from project creation to deployment. cargo handles various tasks, including managing project dependencies specified in Cargo.toml, downloading and compiling crates from crates.io, building the project, running tests, generating documentation, and publishing new versions of libraries and applications. It provides a consistent and reliable workflow, ensuring that all necessary components are correctly assembled and linked, significantly simplifying the complex build processes often associated with compiled languages. Its comprehensive feature set makes it central to the Rust ecosystem.

CAVEATS

cargo is not a standard Linux command but part of the Rust toolchain. It requires the Rust programming language to be installed on the system, typically via rustup. While powerful, cargo operations can sometimes consume significant disk space and build times, especially for large projects with numerous dependencies. Dependency resolution conflicts can occasionally occur, requiring manual intervention or version adjustments in Cargo.toml.

<B>WORKSPACE SUPPORT</B>

cargo natively supports workspaces, allowing multiple related Rust packages (crates) to be managed and built together from a single top-level Cargo.toml file. This feature is crucial for larger projects composed of several interdependent components, simplifying their development, ensuring consistent builds across all crates within the workspace, and enabling shared dependencies and build profiles.

HISTORY

cargo was developed alongside the Rust programming language itself, driven by the crucial need for a robust and consistent build and dependency management system. Its initial development began around 2012-2013, predating Rust's 1.0 stable release in 2015. It quickly became an integral part of the Rust developer experience, ensuring that projects could be easily shared, built, and maintained across different environments. Its adoption significantly contributed to Rust's growth and widespread usage, effectively replacing earlier, more ad-hoc build solutions in the Rust ecosystem.

SEE ALSO

rustc(1): The Rust compiler., rustup(1): The Rust toolchain installer and manager., make(1): A general-purpose build automation tool., cmake(1): A cross-platform build system generator.

Copied to clipboard