Sub topic 1
Packages, crates, and what Cargo builds
This chapter explains the vocabulary Rust uses for project structure and what Cargo does when you run cargo build. Later chapters build on these terms: modules, visibility, dependencies, workspaces, and tests.
Prerequisites
This chapter assumes you can:
- Read basic Rust syntax: functions, structs, enums, and
Result. - Explain ownership and borrowing at an introductory level.
- Run
cargo buildandcargo runfrom a terminal.
You do not need to know anything about modules yet. They are the subject of the next chapter.
Three words that are easy to confuse
Rust uses package, crate, and module for different layers of organization.
- A crate is the unit the Rust compiler (
rustc) compiles. One crate produces one output: a library or an executable. The compiler starts from a single source file, called the crate root, and followsmoddeclarations to find the rest of the code. - A package is the unit Cargo manages. It is a directory with a
Cargo.tomlfile that describes one or more crates and how to build them. - A module is a namespace inside a crate. It groups items such as functions and structs and controls which of them are visible to other code.
The relationship is nested: a package contains crates, and a crate contains modules.
People often say "crate" when they mean "package". For example, "add the serde crate to your project" refers to a package on crates.io. Most packages published there contain one library crate, so the shortcut rarely causes trouble. It matters when a package holds several crates and you need to say which one you mean.
What a package looks like
Create a package with cargo new:
cargo new my_geometry --libCargo generates this layout:
my_geometry/
├── Cargo.toml
└── src/
└── lib.rsThe manifest, Cargo.toml, is minimal:
[package]
name = "my_geometry"
version = "0.1.0"
edition = "2021"The edition field selects a set of language rules. It is set per crate, and crates with different editions can be linked together.
How Cargo finds crate roots
Cargo follows conventions so that you rarely have to list your crates in Cargo.toml.
| File | Meaning |
|---|---|
| `src/main.rs` | Crate root of a binary crate with the same name as the package |
| `src/lib.rs` | Crate root of a library crate with the same name as the package |
| `src/bin/*.rs` | Each file is the crate root of an additional binary crate |
| `tests/*.rs` | Each file is the crate root of an integration test crate |
| `examples/*.rs` | Each file is the crate root of an example binary |
| `benches/*.rs` | Each file is the crate root of a benchmark |
A package follows these rules:
- It can contain at most one library crate.
- It can contain any number of binary crates.
- It must contain at least one crate, library or binary.
If you need a different layout, you can override the defaults with [lib] and [[bin]] tables in Cargo.toml. Following the conventions is usually better, because other Rust developers know where to look.
What Cargo does when you build
Cargo is a build system and package manager. It does not compile Rust itself. It prepares the inputs and calls rustc once per crate.
When you run cargo build, Cargo performs these steps:
- Reads
Cargo.tomland finds the crates in the package. - Resolves dependencies. It picks a version of each dependency that satisfies every requirement, and records the choice in
Cargo.lock. - Downloads the source of any dependency it does not already have.
- Compiles each crate in dependency order, running
rustcwith the right flags, features, and--externreferences to the compiled dependencies. - Writes the results to the
targetdirectory.
Cargo skips work when nothing has changed. A second cargo build with no edits finishes almost immediately, because Cargo detects that each crate's inputs are the same as before.
Where the output goes
Build output lives in target/. The subdirectory depends on the profile:
target/debug/holds output fromcargo build. This build compiles quickly and includes debug information.target/release/holds output fromcargo build --release. This build is optimized and takes longer to compile.
A binary crate produces an executable named after the crate, such as target/debug/my_geometry. A library crate produces an .rlib file in target/debug/, plus intermediate files in target/debug/deps/. You normally never use the .rlib directly. Cargo passes it to rustc when another crate depends on it.
Cargo.lock
Cargo.lock records the exact version of every dependency in the build. It makes builds repeatable: the same lock file gives the same dependency versions on every machine. The usual practice is to commit Cargo.lock for packages that produce executables. For libraries, teams differ, because downstream users resolve their own versions regardless of your lock file.
A package with both crate types
A package can have both src/lib.rs and src/main.rs. Cargo then builds two crates, both named my_geometry, in one package. The binary uses the library as an ordinary dependency.
my_geometry/
├── Cargo.toml
└── src/
├── lib.rs
└── main.rsHere is a small, runnable example. First the library:
// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Then the binary, which refers to the library by the package name:
// src/main.rs
fn main() {
println!("{}", my_geometry::add(2, 3));
}Run it with cargo run, and the program prints 5.
The binary crate does not see the library's private items. It can only use what the library marks pub. This is the same boundary that an external user of your library would face, which is why putting logic in lib.rs and keeping main.rs thin makes the code easier to test. A later chapter covers this layout in detail.
Inspecting what Cargo sees
Two commands help you check your understanding of a package:
cargo metadata --no-deps --format-version 1
cargo build --verbosecargo metadataprints JSON that lists every package and its targets (crates) with their root file paths.cargo build --verboseshows the actualrustcinvocations, one per crate. You can see the--crate-type,--crate-name, and--externflags that Cargo supplies.
Common mistakes
- Using the package name with a hyphen in code. If the package is named
my-geometry, the crate name in Rust code ismy_geometry. Cargo replaces hyphens with underscores. - Expecting a library to be usable from
main.rswithout its name. The binary crate and the library crate are separate crates. Items fromlib.rsneed a path that starts with the library name, or ausedeclaration. - Putting a second library in one package. A package allows only one library crate. Use a workspace for more, which is covered in a later chapter.
- Editing
Cargo.lockby hand. Let Cargo update it withcargo updateor by adding dependencies.
Summary
A crate is what rustc compiles. A package is what Cargo manages, and it holds at most one library crate and any number of binary crates. Cargo finds crate roots by convention, resolves dependencies into Cargo.lock, and calls rustc once per crate. Modules, covered next, organize code inside a crate.
Exercise
A package contains src/lib.rs, src/main.rs, and src/bin/tool.rs. How many crates does Cargo build for it, and which of them can be libraries?
Show answer
Three crates: the library crate from src/lib.rs, the binary crate from src/main.rs, and the binary crate from src/bin/tool.rs. Only the one from src/lib.rs is a library, because a package can have at most one library crate.
Exercise
You run cargo build twice without changing any file. Which file records the dependency versions that make both builds use the same versions, and in which directory is the compiled output?
Show answer
Cargo.lock records the exact dependency versions. The compiled output is in the target directory, under target/debug for a default build.