# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/build-languages/cargo/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Create & Build

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

```bash
cargo new my-app
```

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

```bash
cargo new --lib my-lib
```

`cargo init` — Initialize a project in the current directory.

```bash
cargo init
```

`cargo build` — Compile the project in debug mode.

```bash
cargo build
```

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

```bash
cargo build --release
```

`cargo run` — Build and run the project.

```bash
cargo run
```

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

```bash
cargo run -- --port 8080
```

## Dependencies

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

```bash
cargo add serde
```

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

```bash
cargo add tokio@1.35
```

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

```bash
cargo add serde --features derive
```

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

```bash
cargo add --dev criterion
```

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

```bash
cargo remove regex
```

`cargo update` — Update dependencies to latest compatible versions.

```bash
cargo update
```

`cargo tree` — Display the dependency tree.

```bash
cargo tree
```

## Test & Check

`cargo test` — Run all tests.

```bash
cargo test
```

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

```bash
cargo test test_parse
```

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

```bash
cargo test -- --nocapture
```

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

```bash
cargo check
```

`cargo bench` — Run benchmarks.

```bash
cargo bench
```

`cargo clippy` — Run the Clippy linter for code improvements.

```bash
cargo clippy -- -W clippy::pedantic
```

## Format & Documentation

`cargo fmt` — Format code using rustfmt.

```bash
cargo fmt
```

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

```bash
cargo fmt -- --check
```

`cargo doc` — Generate documentation for the project.

```bash
cargo doc
```

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

```bash
cargo doc --open
```

## Publish & Install

`cargo publish` — Publish the crate to crates.io.

```bash
cargo publish
```

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

```bash
cargo publish --dry-run
```

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

```bash
cargo install ripgrep
```

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

```bash
cargo install --path .
```

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

```bash
cargo uninstall ripgrep
```

## Workspaces & Clean

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

```bash
cargo clean
```

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

```bash
cargo test --workspace
```

`cargo metadata` — Output project metadata as JSON.

```bash
cargo metadata --format-version 1
```

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

```bash
cargo search http server
```

<!-- PROSE:outro -->
## 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

- [The Cargo Book](https://doc.rust-lang.org/cargo/) – official Cargo documentation with reference and guides
- [crates.io](https://crates.io/) – the central registry for Rust crates
- [Rust – official website](https://www.rust-lang.org/) – getting started, installation and learning resources
<!-- PROSE:outro:end -->

## Related Commands

- [artisan](https://www.jpkc.com/db/en/cheatsheets/build-languages/artisan/) – command-line tool for the Laravel PHP framework
- [composer](https://www.jpkc.com/db/en/cheatsheets/build-languages/composer/) – dependency manager for PHP projects
- [drush](https://www.jpkc.com/db/en/cheatsheets/build-languages/drush/) – command-line shell for Drupal

