Sub topic 1
Packages, crates, and what Cargo builds
Prerequisites
This chapter assumes you can write basic Rust (functions, structs, enums, Result), understand ownership at an introductory level, and run cargo build and cargo run. Nothing else is needed.
Three words, three levels
Rust uses package, crate, and module for different levels of organization. People often mix them up.
- A crate is the unit the Rust compiler (
rustc) compiles in one run. It is either a library or an executable (a binary). - A package is what Cargo manages. It is described by one
Cargo.tomlfile and contains one or more crates. - A module is a namespace inside a crate. Modules are the subject of the next chapter.
So cargo new creates a package, and the package produces crates.
How Cargo finds crates
Cargo follows conventions, so you rarely list source files yourself. A package can look like this:
my_geometry/
├── Cargo.toml
└── src/
├── lib.rs
├── main.rs
└── bin/
└── tool.rs| File | Crate it defines | Crate name |
|---|---|---|
| `src/lib.rs` | Library crate (at most one per package) | `my_geometry` |
| `src/main.rs` | Binary crate | `my_geometry` |
| `src/bin/tool.rs` | Extra binary crate | `tool` |
The file you name is the crate root. The compiler starts reading there, and everything else in the crate is reached from it.
The manifest
Cargo.toml is the manifest. Its [package] table gives the package its identity:
[package]
name = "my_geometry"
version = "0.1.0"
edition = "2021"The name becomes the library crate's name. Hyphens in a package name become underscores in code, so a package called my-geometry is used as my_geometry. The edition selects a set of language rules, and each crate in a package follows it.
What Cargo builds
Cargo does not call rustc once per package. It works out the list of crates, then invokes rustc for each one in dependency order.
- It reads
Cargo.tomland finds the crate roots by the conventions above. - It resolves dependencies and writes the exact versions to
Cargo.lock. - It compiles each dependency as a library crate, then your library, then your binaries.
- It places the results in
target/debug, ortarget/releasewith--release.
A binary in the same package can use the package's library crate by its name. Here is a minimal src/lib.rs and src/main.rs pair:
// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}// src/main.rs
fn main() {
println!("{}", my_geometry::add(2, 3));
}main.rs refers to my_geometry the same way it would refer to any external crate. This is a useful habit. Keep the logic in the library, and keep the binary thin.
cargo build
cargo run
cargo run --bin toolWith more than one binary, cargo run needs --bin to know which one you mean.
Common mistakes
- Calling a package a crate. When someone says "add this crate", they usually mean a package published on crates.io.
- Using the hyphenated package name in code. Use underscores.
- Expecting a new file to be compiled automatically. Only crate roots are found by convention. Other files must be declared as modules, as the next chapter shows.
Exercise
A package has src/lib.rs, src/main.rs, and src/bin/tool.rs. How many crates does it contain, and how do you run the one from tool.rs?
Show answer
Three crates: one library and two binaries. Run the extra binary with cargo run --bin tool.