By the end of this lesson
Separate a helper behind a module boundary.
Before you start
- Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
- Complete lesson 17, or be comfortable explaining and running its examples.
Three words to know
- Crate
- A unit compiled as a library or executable.
- Module
- A namespace for organizing code inside a crate.
- pub
- A declaration making an item visible beyond its private boundary.
1. Understand the idea
Modules organize names and visibility; they are not separate running processes. Items are private unless exposed where needed. use brings a path into scope; it does not download a package. Keep internal helpers private and expose only the operations that callers need. A separate file is useful as a program grows, but begin with one visible module so the structure is easy to trace.
2. Save, run and observe
Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.
mod reports {
pub fn heading() -> &'static str { "Task report" }
}
use reports::heading;
fn main() { println!("{}", heading()); }
What you should see · example output
Task report
rustc --edition=2024 lesson.rs -o lesson
./lesson3. Explain the result
heading is accessible from main because it is pub inside the reports module. The module itself belongs to the same crate. Removing pub prevents this use from the parent module. In a Cargo binary src/main.rs is normally the crate root.
YOUR TURN
A small check before you continue
Does use reports::heading contact a package registry?
Show a suggested answer
No. It resolves a path in the existing crate. External dependencies are declared in Cargo.toml and handled by Cargo.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
