Sub topic 1
What crates.io and Cargo do for your project
Most Rust programs use code written by other people. This chapter explains where that code lives, what Cargo does with it, and the vocabulary the rest of the tutorial relies on.
Prerequisites
This chapter assumes you have:
- Rust and Cargo installed, so
cargo --versionworks in your terminal. - Basic Rust syntax and some familiarity with modules.
- Experience creating a project with
cargo new. - Comfort running commands in a terminal.
Crates, packages and crates.io
A crate is the unit of compilation in Rust. It is either a library or an executable. A package is what Cargo manages: a directory with a Cargo.toml file that contains one or more crates.
crates.io is the default public registry for Rust packages. When someone publishes a library there, anyone can depend on it by name and version. Published versions are permanent. The registry lets an author yank a version, which stops new projects from selecting it, but it never deletes the code. A project that already depends on a yanked version keeps building.
In everyday usage, "crate" often means the package you download from crates.io, and this tutorial does the same.
What Cargo does for you
Cargo is both the build tool and the package manager. For dependencies it does four jobs:
- Reads your declarations. The
[dependencies]table inCargo.tomllists the crates you want and the versions you accept. - Resolves versions. Cargo picks one concrete version of every crate you use, including the crates your dependencies use. These indirect ones are called transitive dependencies.
- Downloads sources. It fetches the crate files from the registry and caches them on your machine.
- Compiles everything. It builds each dependency, then builds your crate and links them together.
Cargo records its resolution choices in a file called Cargo.lock. A later chapter covers that file in detail.
A first look
Create a project and add two crates. Chapter 3 explains cargo add properly. For now, treat it as a command that edits Cargo.toml for you.
cargo new json_demo
cd json_demo
cargo add serde --features derive
cargo add serde_jsonCargo contacts the registry, picks the newest versions that fit, and writes them into your manifest. Cargo.toml now contains this table:
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"The next program uses both crates. It turns a struct into JSON text and parses it back.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 1, y: 2 };
let json = serde_json::to_string(&p).unwrap();
println!("{json}");
let back: Point = serde_json::from_str(&json).unwrap();
println!("{back:?}");
}Run it with cargo run. The first run prints Compiling lines for serde, serde_json and their own dependencies before it prints your output. The program prints {"x":1,"y":2} and then Point { x: 1, y: 2 }. Later runs skip the dependencies unless something changed.
What this gives you, and what it costs
Dependencies save you from rewriting solved problems such as JSON parsing, HTTP clients and argument handling. A widely used crate has usually been tested on more inputs than your own code will ever see.
Each dependency also adds costs:
- Build time. Cargo compiles every dependency from source, so large dependency trees slow clean builds.
- Trust. You are running code you did not write, and so are the users of your program. This includes build scripts, which run at compile time.
- Maintenance. You must keep up with updates and security fixes in code you do not control.
The final chapters of this tutorial cover how to inspect your tree and how to judge a crate before you adopt it.
Exercise
Exercise
You run cargo add serde_json in a fresh project, then run cargo build. Name the four things Cargo does between reading Cargo.toml and producing your binary, in order.
Show answer
It reads the declared dependencies, resolves concrete versions (writing them to Cargo.lock), downloads the crate sources from crates.io, and compiles the dependencies followed by your own crate.