Modules, crates and visibility
Modules, crates and visibility
A growing school application needs boundaries between reporting rules and other code. A module organizes names and controls access. A crate is a compilation unit; a package managed by Cargo can contain a library crate and binary crates. These terms describe different levels of organization.
What you’ll learn
- Expose a public operation while keeping its helper private.
- Distinguish a module, crate and Cargo package.
Modules, crates and visibility in action
Caller uses public label
The public label function uses its private valid helper.
Caller uses public labelModule checks private rule
Main can call label through the imported path, while callers outside grading cannot directly call the private helper.
Module checks private ruleReturn Target met
The returned string literals remain valid for the program’s lifetime.
Return Target met
Modules, crates and visibility explained
Items are generally private unless their visibility allows access. pub exposes an item along an accessible path, and use brings a path into local scope. A private helper can keep implementation details inside a module while a public function supplies the intended operation.
Work through the Rust program
Save the complete source as main.rs in the Rust environment. No standard input is needed.
Rust 2024 · complete program
mod grading {
fn valid(score: u32) -> bool { score <= 100 }
pub fn label(score: u32) -> &'static str {
if !valid(score) { "Invalid" } else if score >= 75 { "Target met" } else { "More practice" }
}
}
use grading::label;
fn main() { println!("{}", label(80)); }Expected output
Target metFollow the execution
The public label function uses its private valid helper. Main can call label through the imported path, while callers outside grading cannot directly call the private helper. The returned string literals remain valid for the program’s lifetime.
Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.
Try a small variation
Your task: Does use make a private helper public?
Rust 2024 · complete program
mod grading {
fn valid(score: u32) -> bool { score <= 100 }
pub fn label(score: u32) -> &'static str {
if !valid(score) { "Invalid" } else if score >= 75 { "Target met" } else { "More practice" }
}
}
use grading::label;
fn main() { println!("{}", label(80)); }Worked answer
No. Use changes how a permitted name is referred to locally; it does not override visibility. Expose the intended API explicitly instead.
Mistakes and limits to recognize
Rust modules do not automatically correspond to every directory. File modules need declarations that establish their place in the tree. Pub(crate) limits exposure to the current crate. Keep sensitive data protection separate from language visibility rules.
Check your understanding
Take the idea into your next program
No. Use changes how a permitted name is referred to locally; it does not override visibility. Expose the intended API explicitly instead. Rust modules do not automatically correspond to every directory.
Further reading: the official Rust Book on modules, crates and visibility.
Reaching the recap and completing the quizzes are separate milestones.