Modules and pub
What you'll learn:
- split functions into modules and open the ones you need with
pub; - name the path to a function with
::,crate::anduse; - add someone else's crate with a line in
Cargo.tomland read the version requirement"1.0"; - write tests with
#[test]andassert_eq!and run them withcargo test.

Picture a café building. Inside are rooms: a kitchen, a pantry, a dining hall. Each room has its own door, and by default the door is locked. A guest in the corridor gets only into rooms whose door is open, while a cook inside the kitchen moves around it freely.
A Rust program is built the same way. The whole building is a crate: a program or a library that the compiler builds in one go. The rooms are modules, named parts of a crate that hold functions, structs and constants.
Why have rooms at all? While a program fits on a screen, you can write its functions one after another in main.rs. Once there are a hundred of them, the one you need is hard to find, and two functions named clean can no longer sit side by side. A module gives functions a shared name and decides which of them are visible from outside.
A module is a room in a crate
You declare a module with the word mod, followed by a name and the contents in curly braces:
mod kitchen {
pub fn cook() {
println!("cooking");
}
}
fn main() {
kitchen::cook(); // cooking
}Let's go through the program line by line:
mod kitchen { … }declares the modulekitchenright in this file.pub fn cook()is a function inside the module. The wordpubopens it to code outside.kitchen::cook()inmainis the path to the function: the module name, then::, then the function name.
A path reads like an address in the building: "room kitchen, and in it cook". You have written such a path before: in std::collections::HashMap, std is the standard library crate and collections is a module inside it.
Everything is private until pub
Everything inside a module is private by default: visible only to code in that same module and in modules nested inside it. The rule covers functions, structs, constants and the nested modules themselves. The word pub opens the door to the corridor: the item becomes visible wherever its module is visible.
Step through the program and watch the frame names: each one is the full path to the function through the modules.
1mod kitchen {2 pub fn cook() {3 println!("cooking");4 wash();5 }6 7 fn wash() {8 println!("washing");9 }10 11 pub mod pantry {12 pub fn jars() -> u32 {13 314 }15 }16}17 18fn main() {19 println!("start");20 kitchen::cook();21 let n = kitchen::pantry::jars();22 println!("jars: {n}");23}
main runs in the crate root, outside every module.
What the example showed:
mainlives in the crate root and reachescookby the pathkitchen::cook, becausecookis markedpub.washis private, butcooksits in the same module and calls it by its short name.- The path to
jarsgoes through two doors:pub mod pantryandpub fn jars. Without eitherpub, the path is closed. maincannot callkitchen::wash(): that is trying to walk into a locked room.
Here the building analogy breaks down. A locked door can be forced while the café is open, but Rust's privacy is checked by the compiler before the program ever runs. A program that calls a private function from outside does not build, so there is no such check left while it runs.
Paths and use
You can start a path at the crate root with the word crate: crate::kitchen::cook(). This is an absolute path, like an address counted from the main entrance. A path without crate:: starts at the current module, and in main the current module is the root. A path can also start with the name of another crate, as in std::collections::HashMap.
A long path is tiresome to repeat. use puts up a signpost — it creates a short name for a path:
mod kitchen {
pub mod pantry {
pub fn jars() -> u32 {
3
}
}
}
use crate::kitchen::pantry;
fn main() {
println!("{}", crate::kitchen::pantry::jars()); // 3
println!("{}", pantry::jars()); // 3
}use crate::kitchen::pantry;makes the namepantrya short name for the module at that path.- The first
println!names the function by its full path from the root. - The second follows the signpost:
pantry::jars()is the very same function.
use does not copy or include code; it only adds a name. The custom is to import the module and call the function through it: pantry::jars() shows at once where the function comes from. The line use std::collections::HashMap; from the collections theme does the same for a type.
Common mistake
A function in a module is called from outside, and pub is forgotten:
mod kitchen {
pub fn cook() {
println!("cooking");
}
fn wash() {
println!("washing");
}
}
fn main() {
println!("start");
kitchen::cook();
kitchen::wash(); // ✗
}error[E0603]: function `wash` is privateThe module sits in the same file as main, but that changes nothing: without pub, the function is visible only inside kitchen. The program does not even print start. The compiler checks the whole program before it runs, and since the build failed, there is nothing to run.
There are two fixes. If the function really must be called from outside, write pub fn wash(). If not, call it from inside the module, the way cook does in the example above.
Interviewers like to ask "who can see an item without pub". The answer: only its own module and the modules nested inside it.
Key points
- A module
mod name { … }is a named part of a crate; you reach its items by the pathname::item. - Everything in a module is private until marked
pub; a nested module on the path needspubtoo. - Calling a private function from outside is error E0603 at build time, and the program does not run.
usecreates a short name for a path and copies nothing.