What is Cargo?
Cargo is Rust’s build system and package manager. Most Rustaceans use this tool to manage their Rust projects because it handles many tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries.
The Cargo.toml File
This file is the "Manifest" for your project. It contains all the metadata and dependencies.
toml
Release Profiles
Wait! Rust has two main ways to build your code:
- Debug (
cargo build): Fast compilation, but the resulting code is slow (includes debug symbols). - Release (
cargo build --release): Slow compilation (optimizing everything), but the resulting code is blazing fast.
Workspaces
For large projects with multiple libraries and executables, you can use Workspaces to manage them all in one repository.
toml
Features
You can define "Features" to allow users of your library to only compile the parts they need. This keeps binary sizes small and compilation fast.
toml
Publishing to Crates.io
If you want to share your code with the world, you can publish it to crates.io using
cargo publish.[!TIP] Usecargo doc --opento automatically generate and open the documentation for your project and all of its dependencies in your browser!
Summary
| Command | Purpose |
|---|---|
| cargo new | Create a new project |
| cargo build | Compile (Debug mode) |
| cargo run | Build and Run |
| cargo check | Check for errors (Fastest) |
| cargo test | Run unit and integration tests |
| cargo doc | Generate documentation |
| cargo update | Update dependencies in Cargo.lock |
| Crate | A package of Rust code (Library or Binary) |
| Registry | crates.io (Where crates live) |
| Manifest | Cargo.toml (The project settings) |
| Lockfile | Cargo.lock (Precise versions for reproducible builds) |
| Safety | Cargo ensures everyone builds with exactly the same library versions |
| Standard | Edition system ensures smooth updates over time |