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
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.ioExplanation
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
// 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
$ 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: 57Key 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
1. What is the purpose of Cargo.lock?
2. Which command compiles and immediately runs the resulting binary?
3. What is crates.io?
4. In Rust terminology, what is a 'crate'?
5. Where are a project's dependencies declared?
Was this page helpful?
You May Also Like
Testing in Rust
Learn Rust's built-in testing framework, from #[test] functions and assertion macros to unit and integration test organization.
Setting Up a Rust Environment
Step-by-step guide to installing Rust with rustup and creating your first Cargo project.
Introduction to Rust Programming
A beginner-friendly overview of Rust, a systems programming language built for memory safety and speed.
Error Handling in Rust (Result)
Learn how Rust models recoverable errors with the Result enum and the ? operator instead of exceptions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics