Crates and modules
A crate is the unit Rust compiles at once, either a library or a binary. Modules are the named tree of namespaces inside a crate that organise its items and control what is visible where.
Detailed Explanation
Start with the crate. When you run cargo build, the compiler is handed one root source file and follows it to every other file it needs. Everything it reaches is compiled together as a single unit: that unit is a crate. A package (one Cargo.toml) can contain at most one library crate, at src/lib.rs, and any number of binary crates, at src/main.rs and src/bin/*.rs.
This differs sharply from C and C++, where each .cpp file is compiled separately into an object file and the linker stitches them together afterwards. Because Rust compiles a whole crate at once, it can inline and optimise across your files freely, and it never needs header files or forward declarations. Order does not matter: a function at the bottom of a file can call one at the top, and a module declared late can be used early. The cost is granularity. Change one line and the whole crate is recompiled, which is why large projects split into several crates in a workspace to get parallel and incremental builds.
Inside a crate, modules form a tree rooted at crate. You create a module with mod. There are two forms. An inline module puts the body in braces right there. A file module, mod network;, tells the compiler to load the body from another file.
This is the part that surprises people: mod is a declaration, not an import, and the module tree is not the filesystem. A .rs file sitting in src/ is not part of your crate until some module declares it with mod. If you create src/network.rs and never write mod network; in src/lib.rs, the compiler never reads that file. Nothing in it compiles, and nothing in it errors.
The file layout rules for mod thing; declared inside module parent:
src/parent/thing.rs— the modern layout, preferred since the 2018 edition.src/parent/thing/mod.rs— the older layout, still supported. Use it whenthinghas children and you prefer the folder to be self-contained.
Do not mix both for the same module; the compiler rejects that as ambiguous.
You would reach for modules as soon as one file stops fitting in your head. A typical parser crate might have lexer, ast, and error modules. The real motivation, though, is not tidiness but privacy: a module is the boundary that pub is measured against, so splitting code into modules is how you decide which internals stay internal.
The costs are modest and mostly cognitive. Deep module trees make paths long and force more use statements. A very large crate compiles slowly as one unit. A good default is a shallow tree of a few meaningful modules, and a split into separate crates only when build time or reuse demands it.
Code Examples
src/lib.rs — the crate root of a library crate.
// src/lib.rs — the crate root of a library crate.
mod network; // loads src/network.rs; without this line that file is never compiled
// An inline module. Its body lives right here.
pub mod config {
pub struct Settings {
pub port: u16,
}
// Paths are relative by default, so `Settings` resolves inside this module.
pub fn default_settings() -> Settings {
Settings { port: 8080 }
}
}
pub fn start() {
let settings = config::default_settings();
// `network` is private, but `start` is inside the crate root, so it may use it.
network::listen(settings.port);
}src/network.rs — the body of the `network` module.
// src/network.rs — the body of the `network` module.
// Note: this file does NOT repeat `mod network`. The declaration lives in the parent.
pub fn listen(port: u16) {
println!("listening on {port}"); // prints: listening on 8080
}
mod tcp {
pub fn handshake() {}
}
pub fn open() {
tcp::handshake(); // relative path: a child of the current module
self::tcp::handshake(); // the same thing, written explicitly
crate::start(); // absolute path from the crate root
}Common Gotchas
Adding a file is not enough
creating `src/parser.rs` without writing `mod parser;` in the parent means the compiler never sees it. When you then call `parser::parse()`, you get `error[E0433]: failed to resolve: use of undeclared crate or module 'parser'` even though the file is right there.
A package with both `src/main.rs` and `src/lib.rs` has two crates
the binary does not automatically see the library's private items. It depends on the library by package name, so `main.rs` writes `use my_package::thing;` and can only reach `pub` items.
`mod` twice creates two unrelated modules
declaring `mod helpers;` in both `lib.rs` and `network.rs` produces `crate::helpers` and `crate::network::helpers`, two separate modules with two separate copies of every type. To reuse one module elsewhere, import it with `use`, do not re-declare it.
`#[cfg(test)] mod tests` is the idiomatic unit-test home
a child module can see its parent's private items, which is why unit tests live inside the file they test rather than beside it.