Modules, crates, and visibility.
Understand how a Rust program names and protects its boundaries.
The idea behind the syntax.
A crate is a compilation unit, and a package can contain a library crate, binary crates, or both. Modules organise names inside a crate. Paths such as crate::domain::Task locate items, while use brings a path into local scope for readability. This system is more than file layout: it defines which details are public and which remain implementation choices.
Items are private by default. pub exposes an item through its containing module boundaries; a public field or function therefore becomes a promise to callers. Keeping a field private lets the type enforce its own invariant through methods. A library's API should reveal the concepts a caller needs, not every helper used to implement them. The compiler helps enforce that separation by rejecting access to private items.
What Rust is checking.
Cargo.toml declares package metadata, edition, features, and dependencies. Cargo.lock records resolved dependency versions, especially important for reproducible applications. The module tree and file tree are related, but a module's meaning comes from declarations and paths, not from a file's name alone. Re-exports with pub use can offer a stable public path while internal modules change.
When reading unfamiliar Rust code, begin at the crate root, follow public paths, then look at the impl blocks and trait implementations behind them. This is often faster than reading files in alphabetical order. A clear boundary lowers the cost of changing internals because callers depend on a small, deliberate surface. The examples below illustrate that the organisation of code is itself a design decision.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Modules, privacy & use
A crate is a compilation unit. A package may contain a library crate and binary crates. Modules organize names and privacy within a crate; use brings a path into scope but does not download a dependency. Items are private by default, and pub exposes an item according to its containing module visibility. Real packages can place modules in separate files using mod declarations. Keep a small public surface and put implementation details behind functions so internal refactoring does not force every caller to change.
mod pricing {
fn tax(cents: u32) -> u32 {
cents / 10
}
pub fn total(cents: u32) -> u32 {
cents + tax(cents)
}
}
use pricing::total;
fn main() {
println!("{}", total(1000));
}1100
- tax remains an implementation detail.
- total is explicitly public.
- use introduces the function name in the caller’s scope.
The boundary: pub on a field or function does not bypass every private containing module. Visibility is determined along the path.
Cargo manifests & dependencies
Cargo.toml declares package metadata, an edition, and dependencies. Cargo.lock records resolved dependency versions for reproducibility and should be committed for these executable workbook projects. An edition selects language rules and migration boundaries; it is not the compiler version. cargo check type-checks without a final executable, cargo test runs tests, cargo fmt formats, and cargo clippy offers additional diagnostics. Features are additive configuration switches: inspect which optional capabilities a dependency enables instead of adding every feature by default.
fn main() {
let manifest = "[package]\nname = \"first-build\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
println!(
"{}",
manifest
.lines()
.find(|line| line.starts_with("edition"))
.unwrap()
);
}edition = "2024"
- A manifest is configuration rather than Rust source.
- The workbook generates a proper manifest per lesson.
- Run cargo check, cargo test, and cargo fmt --check from the workspace root as appropriate.
The boundary: A semver requirement such as 1 allows compatible updates. A committed lockfile pins the actual resolved graph until updated.
Keep this model.
Modules manage names; visibility manages promises. A useful API exposes a stable concept while leaving its internal structure free to evolve.