Cargo — Rust's Package Manager and Build Tool

Practical guide to Cargo: create, build and test Rust projects, manage dependencies, generate documentation and publish crates to crates.io.

Cargo is Rust's official build tool and package manager – a single command set lets you scaffold projects, pull dependencies (crates) from crates.io, compile, test and publish. Instead of juggling compiler, linker and dependency resolution by hand, you describe your project in Cargo.toml and let Cargo handle the rest. This guide walks you through the commands you reach for daily – from a fresh project through tests and linting to publishing.

Create & Build

cargo new <name> — Create a new binary project with Git repository.

cargo new my-app

cargo new --lib <name> — Create a new library project.

cargo new --lib my-lib

cargo init — Initialize a project in the current directory.

cargo init

cargo build — Compile the project in debug mode.

cargo build

cargo build --release — Compile with optimizations for release.

cargo build --release

cargo run — Build and run the project.

cargo run

cargo run -- <args> — Run with arguments passed to the binary.

cargo run -- --port 8080

Dependencies

cargo add <crate> — Add a dependency to Cargo.toml.

cargo add serde

cargo add <crate>@<version> — Add a specific version.

cargo add tokio@1.35

cargo add <crate> --features <feat> — Add with specific features enabled.

cargo add serde --features derive

cargo add --dev <crate> — Add as a dev dependency.

cargo add --dev criterion

cargo remove <crate> — Remove a dependency from Cargo.toml.

cargo remove regex

cargo update — Update dependencies to latest compatible versions.

cargo update

cargo tree — Display the dependency tree.

cargo tree

Test & Check

cargo test — Run all tests.

cargo test

cargo test <name> — Run tests matching a name pattern.

cargo test test_parse

cargo test -- --nocapture — Run tests with stdout/stderr visible.

cargo test -- --nocapture

cargo check — Check for errors without compiling (faster than build).

cargo check

cargo bench — Run benchmarks.

cargo bench

cargo clippy — Run the Clippy linter for code improvements.

cargo clippy -- -W clippy::pedantic

Format & Documentation

cargo fmt — Format code using rustfmt.

cargo fmt

cargo fmt -- --check — Check formatting without making changes.

cargo fmt -- --check

cargo doc — Generate documentation for the project.

cargo doc

cargo doc --open — Generate and open documentation in browser.

cargo doc --open

Publish & Install

cargo publish — Publish the crate to crates.io.

cargo publish

cargo publish --dry-run — Check if the crate is ready to publish.

cargo publish --dry-run

cargo install <crate> — Install a binary crate from crates.io.

cargo install ripgrep

cargo install --path . — Install the current project as a binary.

cargo install --path .

cargo uninstall <crate> — Uninstall a previously installed binary crate.

cargo uninstall ripgrep

Workspaces & Clean

cargo clean — Remove the target directory (compiled artifacts).

cargo clean

cargo workspace <cmd> — Run a command across all workspace members.

cargo test --workspace

cargo metadata — Output project metadata as JSON.

cargo metadata --format-version 1

cargo search <query> — Search for crates on crates.io.

cargo search http server

Conclusion

Cargo takes the routine out of any Rust project: cargo build, cargo test and cargo run cover the daily development loop, while cargo clippy and cargo fmt keep your code clean. Two commands deserve deliberate care: cargo install downloads and compiles third-party crates along with their build scripts, which run on your machine – so only install what you trust. And cargo publish is final: once a version is published it cannot be deleted or overwritten on crates.io – check first with --dry-run.

Further Reading

  • artisan – command-line tool for the Laravel PHP framework
  • composer – dependency manager for PHP projects
  • drush – command-line shell for Drupal