Skip to content
Theory · Modules, crates and tests · lesson 1 of 6

Modules and pub

What you'll learn

What you'll learn:

  • split functions into modules and open the ones you need with pub;
  • name the path to a function with ::, crate:: and use;
  • add someone else's crate with a line in Cargo.toml and read the version requirement "1.0";
  • write tests with #[test] and assert_eq! and run them with cargo test.
On the plan of the café crate the pantry and the kitchen are locked, but the kitchen's serving hatch pub opens onto the corridor

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:

Rust
mod kitchen {
    pub fn cook() {
        println!("cooking");
    }
}

fn main() {
    kitchen::cook(); // cooking
}

Let's go through the program line by line:

  1. mod kitchen { … } declares the module kitchen right in this file.
  2. pub fn cook() is a function inside the module. The word pub opens it to code outside.
  3. kitchen::cook() in main is 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.

LiveA path through the roomsstep 1 of 7 · frames: 1

main runs in the crate root, outside every module.

Watch the frame name: it is the path to the function through the modules

What the example showed:

  1. main lives in the crate root and reaches cook by the path kitchen::cook, because cook is marked pub.
  2. wash is private, but cook sits in the same module and calls it by its short name.
  3. The path to jars goes through two doors: pub mod pantry and pub fn jars. Without either pub, the path is closed.
  4. main cannot call kitchen::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:

Rust
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
}
  1. use crate::kitchen::pantry; makes the name pantry a short name for the module at that path.
  2. The first println! names the function by its full path from the root.
  3. 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:

Rust
mod kitchen {
    pub fn cook() {
        println!("cooking");
    }

    fn wash() {
        println!("washing");
    }
}

fn main() {
    println!("start");
    kitchen::cook();
    kitchen::wash(); // ✗
}
TEXT
error[E0603]: function `wash` is private

The 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 path name::item.
  • Everything in a module is private until marked pub; a nested module on the path needs pub too.
  • Calling a private function from outside is error E0603 at build time, and the program does not run.
  • use creates a short name for a path and copies nothing.