100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Cargo and Crates in Rust

Understand Cargo, Rust's build system and package manager, and how crates and Cargo.toml/Cargo.lock manage dependencies.

Concurrency & ToolingBeginner8 min readJul 8, 2026
Analogies

Introduction

Cargo is Rust's official build system and package manager. It compiles your code, downloads and manages dependencies, runs tests, generates documentation, and publishes packages. A 'crate' is Rust's unit of compilation — it can be a binary crate (an executable) or a library crate (reusable code). Crates.io is the central registry where the Rust community publishes and shares open-source crates, similar to npm for JavaScript or PyPI for Python.

🏏

Cricket analogy: Cargo acts like a franchise's management office that recruits players (dependencies), schedules practice matches (tests), and prints the match programme (docs); a crate is like a single squad, either the touring XI (binary) or the academy roster others draft from (library).

Syntax

bash
cargo new my_project      # scaffold a new binary crate
cargo build                # compile the project (debug mode)
cargo build --release      # compile with optimizations
cargo run                  # build and run the binary
cargo test                 # run tests
cargo doc --open           # generate and view documentation
cargo publish               # publish a crate to crates.io

Explanation

cargo new creates a new project with a standard layout: a Cargo.toml manifest and a src/main.rs (or src/lib.rs for a library). Cargo.toml declares package metadata (name, version, edition) and a [dependencies] section listing crates the project needs, using semantic versioning ranges such as "1.0" or "^1.2". When you build the project, Cargo resolves those version ranges to exact versions and records them in Cargo.lock, ensuring that everyone who builds the project — and every future build — uses identical dependency versions until the lock file is intentionally updated. cargo build compiles the crate and its dependency graph, cargo run additionally executes the resulting binary, cargo test runs any functions annotated with #[test], cargo doc builds HTML documentation from doc comments, and cargo publish uploads a versioned crate to crates.io for others to depend on.

🏏

Cricket analogy: cargo new is like drafting a fresh squad roster template with a team charter (Cargo.toml) listing player names and squad numbers, while a lockfile fixes the exact playing XI for the season so no substitution changes mid-tournament without approval.

Example

rust
// Cargo.toml
// [package]
// name = "greeter"
// version = "0.1.0"
// edition = "2021"
//
// [dependencies]
// rand = "0.8"

// src/main.rs
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let n: u32 = rng.gen_range(1..=100);
    println!("Random number: {}", n);
}

Output

bash
$ cargo run
   Compiling rand v0.8.5
   Compiling greeter v0.1.0 (/path/to/greeter)
    Finished dev [unoptimized + debuginfo] target(s) in 1.2s
     Running `target/debug/greeter`
Random number: 57

Key Takeaways

  • Cargo is Rust's build system and package manager, handling compilation, dependencies, testing, docs, and publishing.
  • Cargo.toml declares metadata and dependencies; Cargo.lock pins exact resolved versions for reproducible builds.
  • cargo new/build/run/test/doc/publish cover the core project lifecycle.
  • crates.io is the central registry for sharing and downloading open-source Rust crates.
  • A crate is Rust's compilation unit — either a binary (executable) or a library.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#CargoAndCratesInRust#Cargo#Crates#Syntax#Explanation#StudyNotes#SkillVeris