Skip to content

Split a Program into Modules

The Course
•
26 min study

Every program so far has lived in one file. That stops working well once a program has distinct jobs, such as reading input, checking rules and printing a report: a reader looking for the rules has to scroll past everything else. Modules let you split code into named parts, each in its own file if you like, and decide which parts other code may use.

A crate is the unit the compiler builds, one program or one library. Its entry file here is main.rs. The line mod duration; declares a module named duration, and Rust looks for its contents in the neighboring file duration.rs. Items in a module are private by default: only code inside the same module can use them. Writing pub fn makes a function public, so main.rs can call it with a path, duration::total_minutes.

Predict and run

Open both file tabs. What calculation will the call in main.rs ask the helper to do?

Predict, then run Rust
mod duration; fn main() { println!("Total: {} minutes", duration::total_minutes(3, 20)); } pub fn total_minutes(sessions: u32, each: u32) -> u32 { sessions * each }
Check your prediction
Text
Total: 60 minutes

main.rs asks for 3 sessions of 20 minutes each, and the function in duration.rs multiplies them. Notice that duration.rs does not say mod duration itself: the file's name and the mod line in main.rs together make the module.

Remove pub in duration.rs and run. The compiler reports error[E0603]: function total_minutes is private. The function still exists, but main.rs is outside its module and may no longer call it. Restore the keyword.

A module can live in one file

A module does not need its own file. mod name { ... } with a body defines it inline, which is handy for small examples and for seeing the privacy rule in one place. Inside the module, a private helper is free to use; outside it, only the pub items are reachable. A use line brings a path into scope, so you can write a short name instead of the full path every time.

Keep a helper private Rust
mod duration {
    pub fn with_breaks(sessions: u32, each: u32) -> u32 {
        sessions * each + break_time(sessions)
    }

    fn break_time(sessions: u32) -> u32 {
        if sessions > 1 { (sessions - 1) * 5 } else { 0 }
    }
}

use duration::with_breaks;

fn main() {
    println!("One session: {} minutes", with_breaks(1, 25));
    println!("Three sessions: {} minutes", with_breaks(3, 25));
}
Text
One session: 25 minutes
Three sessions: 85 minutes

Three 25-minute sessions take 75 minutes, plus two five-minute breaks between them. main cannot call break_time directly:

Rust
fn main() {
    println!("{}", duration::break_time(3)); // error
}
Text
error[E0603]: function `break_time` is private

That is the point of privacy. The module promises one public function, and it is free to change or remove its private helpers later without breaking any caller. The same rule applies to struct fields: on Day 7 the planner keeps its list of sessions private, so the only way to add one is through a method that checks the rules first.

Make things public on purpose

Start with everything private and add pub only when another module needs the item. A short public surface is easier to understand and harder to misuse.

Practice

Exercise

Your turn

Change the module example to total four 15-minute sessions. Then add a public extra_minutes(total: u32) -> u32 function in duration.rs that adds 5. Call it in main.rs and print With buffer: 65 minutes.

Your working space Rust
mod duration; fn main() { println!("Total: {} minutes", duration::total_minutes(3, 20)); } pub fn total_minutes(sessions: u32, each: u32) -> u32 { sessions * each }
Hint 1: the idea

Keep the calculation in the helper file. The entry file can combine results from multiple calls.

Hint 2: a first step

Bind the result of duration::total_minutes(4, 15) in main. Pass that result to your new public function.

Worked solution

In duration.rs, keep the first function and add the new one. In main.rs, pass the total to the buffer function before printing.

Run the solution Rust
mod duration; fn main() { let total = duration::total_minutes(4, 15); let with_buffer = duration::extra_minutes(total); println!("With buffer: {with_buffer} minutes"); } pub fn total_minutes(sessions: u32, each: u32) -> u32 { sessions * each } pub fn extra_minutes(total: u32) -> u32 { total + 5 }
Text
With buffer: 65 minutes

Four sessions of 15 minutes make 60, and the buffer adds 5. If you forgot pub on extra_minutes, you met E0603 again. Both functions now live behind the duration:: path, so a reader of main.rs can see at a glance which module each calculation comes from.

Was this article helpful?

Your response is saved on this device.