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-appcargo new --lib <name> — Create a new library project.
cargo new --lib my-libcargo init — Initialize a project in the current directory.
cargo initcargo build — Compile the project in debug mode.
cargo buildcargo build --release — Compile with optimizations for release.
cargo build --releasecargo run — Build and run the project.
cargo runcargo run -- <args> — Run with arguments passed to the binary.
cargo run -- --port 8080Dependencies
cargo add <crate> — Add a dependency to Cargo.toml.
cargo add serdecargo add <crate>@<version> — Add a specific version.
cargo add tokio@1.35cargo add <crate> --features <feat> — Add with specific features enabled.
cargo add serde --features derivecargo add --dev <crate> — Add as a dev dependency.
cargo add --dev criterioncargo remove <crate> — Remove a dependency from Cargo.toml.
cargo remove regexcargo update — Update dependencies to latest compatible versions.
cargo updatecargo tree — Display the dependency tree.
cargo treeTest & Check
cargo test — Run all tests.
cargo testcargo test <name> — Run tests matching a name pattern.
cargo test test_parsecargo test -- --nocapture — Run tests with stdout/stderr visible.
cargo test -- --nocapturecargo check — Check for errors without compiling (faster than build).
cargo checkcargo bench — Run benchmarks.
cargo benchcargo clippy — Run the Clippy linter for code improvements.
cargo clippy -- -W clippy::pedanticFormat & Documentation
cargo fmt — Format code using rustfmt.
cargo fmtcargo fmt -- --check — Check formatting without making changes.
cargo fmt -- --checkcargo doc — Generate documentation for the project.
cargo doccargo doc --open — Generate and open documentation in browser.
cargo doc --openPublish & Install
cargo publish — Publish the crate to crates.io.
cargo publishcargo publish --dry-run — Check if the crate is ready to publish.
cargo publish --dry-runcargo install <crate> — Install a binary crate from crates.io.
cargo install ripgrepcargo install --path . — Install the current project as a binary.
cargo install --path .cargo uninstall <crate> — Uninstall a previously installed binary crate.
cargo uninstall ripgrepWorkspaces & Clean
cargo clean — Remove the target directory (compiled artifacts).
cargo cleancargo workspace <cmd> — Run a command across all workspace members.
cargo test --workspacecargo metadata — Output project metadata as JSON.
cargo metadata --format-version 1cargo 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
- The Cargo Book – official Cargo documentation with reference and guides
- crates.io – the central registry for Rust crates
- Rust – official website – getting started, installation and learning resources