Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Modules and Crates  ›  Lesson

Modules, Paths and Privacy

Code Organization & Namespacing 15 minModules group related code into a tree inside a crate, everything is private unless marked pub, and use brings a path into scope under a short name.

Modules, Paths and Privacy

The library program started as one file. Now it has a catalogue, loans, members and reports, and scrolling to find anything takes longer than writing it. Rust's module system lets you split code into named parts, and it decides exactly which parts the rest of the program is allowed to touch.

The rule

Modules form a tree; everything is private until marked pub; use shortens paths.

A crate is one unit the compiler builds: a binary (starting at src/main.rs) or a library (starting at src/lib.rs). A package can hold one library and any number of binaries.

Modules divide a crate into a tree. Declare one inline, or in its own file:

// src/main.rs
mod catalogue;          // loads src/catalogue.rs (or src/catalogue/mod.rs)

fn main() {
    let book = catalogue::Book::new("Iron Tides", 380);
}
// src/catalogue.rs
pub struct Book {
    pub title: String,
    pages: u32,                 // private field
}

impl Book {
    pub fn new(title: &str, pages: u32) -> Book {
        Book { title: title.to_string(), pages }
    }
}

fn tidy(title: &str) -> String { title.trim().to_string() }   // private

A module inside catalogue called search would live in src/catalogue/search.rs, declared with mod search; in catalogue.rs.

Privacy rules:

  • Every item (function, struct, module, constant) is private by default: usable only inside its own module and that module's children.
  • pub makes it visible to the parent and beyond.
  • A pub struct still has private fields unless each field is marked pub. A pub enum makes all its variants public.

Paths name items: crate::catalogue::Book starts from the crate root, super:: goes to the parent module, self:: means the current one.

use creates a shortcut:

use crate::catalogue::Book;
use std::collections::{HashMap, HashSet};

let book = Book::new("Night Garden", 288);

The shortcut

When the compiler says an item "is private", walk the path from the caller to the item and check every link is pub: the module, the type, the function, and any field you touch.

Why can't main read book.pages?Step 1 of 4
  1. Module

    mod catalogue; main is the parent, so the module itself is reachable.

The trap is making every field pub to silence errors. A private field plus a method keeps other code from putting the value into an invalid state.

Drills

Answer fast — trust the rule.

quick drill

With mod catalogue; in src/main.rs, where does Rust look for the module's code?

quick drill

A function in catalogue has no pub. Who can call it?

quick drill

pub struct Book { title: String }. Can main read book.title?

quick drill

Inside catalogue::search, which path reaches Book defined in catalogue?

Remember

  • Modules form a tree inside a crate; mod name; loads name.rs.
  • Everything is private until pub; a public struct's fields still need their own pub.
  • When "is private" appears, check every link in the path; use methods instead of pub fields.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSplit the library program

The library program's main.rs has grown to 800 lines. Explain how you would move the catalogue code into its own module file, what must be marked pub for main.rs to use a Book and its new function, and how use shortens the call.