Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

23.1 Overview

Cargo automates and standardizes many aspects of Rust development. Its core functions include:

  • Project Scaffolding: Creating new library or binary projects (packages) with a consistent directory structure (cargo new, cargo init). cargo new also initializes a Git repository by default, reflecting the common practice of using Git for version control in Rust projects.
  • Dependency Management: Automatically downloading and integrating required packages from Crates.io or other sources (e.g., Git repositories) based on declarations in the Cargo.toml manifest file. These dependencies provide crates that your package can use.
  • Building and Running: Compiling code from crates with different optimization levels (debug vs. release), managing incremental builds, and executing binaries (cargo build, cargo run).
  • Testing and Benchmarking: Discovering and executing tests and benchmarks (cargo test, cargo bench). (Covered in Chapter 24).
  • Packaging and Publishing: Preparing packages for distribution and uploading them to Crates.io (cargo package, cargo publish).
  • Tooling Integration: Acting as a frontend for other development tools like the formatter (cargo fmt), linter (cargo clippy), and documentation generator (cargo doc).

Comparison with C/C++ Build Systems and Package Managers

Coming from C or C++, you might be accustomed to using separate tools:

  • Build Systems: Make, CMake, Meson, Ninja, etc., manage the compilation and linking process. Configuration can be complex, especially for cross-platform projects.
  • Package Managers: Conan, vcpkg, Hunter, or system package managers (like apt, yum, brew) handle external library dependencies. Integrating these with the build system often requires manual effort.

Cargo unifies these roles. It manages both the build process (invoking the Rust compiler rustc with appropriate flags for your crates) and dependency resolution in a single, integrated tool with a consistent interface across all Rust packages. This significantly simplifies project setup and maintenance compared to the fragmented C/C++ ecosystem.