Crates and dependencies
In the previous lesson every room-module stood in one file. A real project is a folder with several files and other people's libraries, and Cargo builds it.
Let's go back to the café building. The building has a passport: it records the name, the version number and the list of ready-made blocks delivered from outside. In a Rust project that passport is the file Cargo.toml.
Package and crate
The command cargo new cafe creates a folder:
cafe
├── Cargo.toml
├── .gitignore
└── src
└── main.rsA package is a folder with a Cargo.toml file, which tells Cargo what to build and how. The .gitignore file is for the Git system and has nothing to do with the build. Cargo generated Cargo.toml itself like this:
[package]
name = "cafe"
version = "0.1.0"
edition = "2024"
[dependencies]Let's go through it line by line:
nameis the package name; the program gets the same name.versionis the version of the package itself.edition = "2024"is the language edition whose rules the compiler reads the code by.[dependencies]is the list of other people's crates, empty for now.
A crate, as you remember, is what the compiler builds in one go: src/main.rs becomes a program, and src/lib.rs, which cargo new --lib creates, becomes a library with no main function.
A module in its own file
A room can move into a separate file. The module kitchen moves to src/kitchen.rs, and in main.rs a single line remains in place of the curly braces:
// src/main.rs
mod kitchen;
fn main() {
kitchen::cook();
}
// src/kitchen.rs
pub fn cook() {
println!("cooking");
}mod kitchen;with a semicolon tells the compiler: "the modulekitchenlives in the filekitchen.rsnext to me".kitchen.rsholds what used to stand inside the curly braces, without the braces themselves.- The path has not changed:
kitchen::cook(), andcargo runprintscooking.
The compiler reads a crate starting from its root and does not scan the src folder on its own. A file joins the crate only when the root refers to it with a mod line.
Dependencies in Cargo.toml
Someone else's crate is a ready-made block delivered from outside. A dependency is a crate your package uses. You write it as a line in the [dependencies] section:
[dependencies]
regex = "1.5"On the build, Cargo downloads the crate regex. Here the picture of ready-made blocks is inexact: what arrives is not a finished part but source code, and Cargo compiles it together with yours. You don't have to type the line by hand: the command cargo add regex adds it for you.
A version is a range
Crate version numbers follow the SemVer rule — three numbers MAJOR.MINOR.PATCH, for example 1.11.2:
PATCHgrows when bugs are fixed;MINORgrows when features are added and nothing breaks;MAJORgrows when the changes may break other people's code.
A version requirement in Cargo.toml most often takes one of three forms: no operator before the number, ~ or =. Interviewers like to ask "what does "1.0" in Cargo.toml mean".
A version with no operator
"1.5" is not an exact version but a requirement "compatible with 1.5". Cargo reads it as ^1.5, and you can also write this form with ^ explicitly. Any version from 1.5.0 up to 2.0.0, not including 2.0.0, fits. Versions 1.5.3 and 1.11.2 fit, while 1.4.9 and 2.0.0 do not. A new major version may break your code, so Cargo will not take it on its own.
Versions starting with 0 have a tighter limit: "0.9" lets in 0.9.x but not 0.10.0.
Tilde ~
"~1.5" lets in patches only: from 1.5.0 up to 1.6.0, not including 1.6.0. Version 1.11.2 no longer fits.
Exact version =
"=1.5.0" allows exactly 1.5.0 and nothing else. You pin a version this way when even a bug fix could get in the way.
Cargo.lock remembers the choice
On the first build, Cargo picks the newest version in the range and writes it into the file Cargo.lock next to Cargo.toml. Later builds take the version from there, even if a newer one is out. This way you and a colleague build the same code, and the command cargo update refreshes the choice within the range.
In short: Cargo.toml says which versions fit, and Cargo.lock says which one was picked. Nobody edits Cargo.lock by hand; Cargo writes it.
Common mistake
The file kitchen.rs was put into src, but the line mod kitchen; was never written in main.rs:
fn main() {
kitchen::cook(); // ✗
}error[E0433]: cannot find module or crate `kitchen` in this scopeThe file sits right there, but it did not join the crate: a module appears only when it is declared with the word mod. The compiler hints at this in its help: line — it suggests adding mod kitchen; to this file. The fix is to write mod kitchen; at the top of main.rs.
Key points
- A package is a folder with
Cargo.toml; a crate is built in one go:src/main.rsgives a program,src/lib.rsa library. - A module in its own file is declared with the line
mod kitchen;, otherwise the file stays out of the build. regex = "1.5"means^1.5: any version from 1.5.0, but never 2.0.0;"~1.5"is patches only,"=1.5.0"is exactly that one.Cargo.lockrecords the picked versions so builds repeat.