Modules and mod
Organize code.
Modules and mod is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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");
}Calling Module Functions
Items inside a module are private by default. To call one from outside, it must be pub, and you reference it with the module path using ::.
mod greetings {
pub fn hello() {
println!("hello!");
}
}
fn main() {
greetings::hello();
}Nested Modules
Modules can contain other modules, forming a tree. The path lists each level separated by ::.
mod app {
pub mod ui {
pub fn draw() {
println!("drawing ui");
}
}
}
fn main() {
app::ui::draw();
}Modules Group More Than Functions
A module can hold functions, structs, enums, constants, and even other modules. This keeps related types and behavior together.
mod shapes {
pub struct Circle {
pub radius: f64,
}
pub const PI: f64 = 3.14159;
}
fn main() {
let c = shapes::Circle { radius: 2.0 };
println!("area = {}", shapes::PI * c.radius * c.radius);
}The Module Tree
Every crate has a root module. Modules you declare form a tree beneath it. Paths describe where an item lives, similar to folders in a file system.
super: The Parent Module
Inside a module, super refers to the parent module. It lets a child call something defined one level up.
fn top_level() {
println!("called via super");
}
mod inner {
pub fn run() {
super::top_level();
}
}
fn main() {
inner::run();
}self: The Current Module
self refers to the current module. It is mostly used to be explicit about where an item lives within a path.
mod tools {
pub fn helper() {
println!("helper running");
}
pub fn run() {
self::helper();
}
}
fn main() {
tools::run();
}Modules in Separate Files
Beyond inline modules, mod network; (with a semicolon, no body) tells Rust to load the contents from network.rs or network/mod.rs.
This is how real projects split code across files while keeping one module tree.
Privacy Boundaries
Modules form privacy boundaries. A child module can see its ancestors' private items, but parents cannot reach into a child's private items.
This encapsulation keeps internal details hidden.
mod bank {
fn secret() -> i32 { 42 }
pub fn reveal() -> i32 { secret() }
}
fn main() {
println!("{}", bank::reveal());
}Organizing Real Projects
Use modules to mirror your domain: auth, db, handlers. Keep each focused, expose a small public surface, and hide the rest.
Clear module boundaries make large codebases navigable.
Quick Check
Test your module knowledge.
Recap
You learned to organize code with modules:
mod name { ... }declares an inline module- Items are private by default; use
pubto expose them - Paths use
::to walk the module tree super,self, andmod name;navigate and split modules
Frequently asked questions
Is the “Modules and mod” lesson free?
Yes — the full text of “Modules and mod” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Modules and mod”?
Organize code. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Modules and mod” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Modules and mod
- pub and Visibility
- use and Paths
- Crates and the Crate Root