Modules and mod
Organize code.
Why Modules?
As programs grow, putting everything in one file becomes messy. Modules let you group related code, control what is public, and avoid name clashes.
The mod keyword declares a module.
Declaring an Inline Module
You can define a module right inside a file with mod name { ... }. Everything between the braces belongs to that module.
mod greetings {
fn hello() {
println!("hello from the module");
}
}
fn main() {
println!("module declared above");
}All lessons in this course
- Modules and mod
- pub and Visibility
- use and Paths
- Crates and the Crate Root