1. What Are Modules in Rust?
A module is a way to organize related functions, structs, enums, and other code together.
For example, in a student management application, we might have:
- Student-related functions
- Teacher-related functions
- Fee-related functions
- Attendance-related functions
Instead of writing everything in one file, we can organize the code into separate modules.
Example:
Output:
Here:
mod studentcreates a module namedstudent.pub fn show_name()defines a public function.student::show_name()calls the function using its module path.
2. Understanding the mod Keyword
The mod keyword is used to declare a module.
Syntax:
Example:
Output:
3. Understanding the pub Keyword
In Rust, items inside a module are private by default.
The pub keyword makes an item accessible from outside its module.
Example:
Important:
Keyword | Meaning |
|---|---|
| Declares a module |
| Makes an item public |
No | Item is private by default |
A child module can access private items of its ancestors, but a parent cannot directly access private items declared inside its child module.
4. Creating Modules in Separate Files
In real-world applications, we usually create separate files for different modules.
Let's create a project named student_app.
Project structure:
Step 1: Create the student.rs file
Inside src/student.rs, write:
Step 2: Declare the module in main.rs
Output:
Explanation:
mod student;tells Rust to load the module fromstudent.rs.student::show_details()calls the function inside the module.puballows these functions to be accessed frommain.rs.
You do not need to write the complete module code inside main.rs.
5. Understanding Module Paths
A module path tells Rust where an item is located.
Rust uses :: to separate module names and item names.
Example:
Output:
The path is:
6. Using the use Keyword
Writing a long module path repeatedly can make code difficult to read.
The use keyword brings a name into the current scope.
Example without use:
Example with use:
Output:
You can also import an entire module:
7. Using self and super
Rust provides special keywords for navigating module paths.
self
self refers to the current module.
Output:
super
super refers to the parent module.
In the example above, super::show_school() accesses the function in the parent school module.
8. Creating Nested Modules in Separate Files
You can also organize modules inside folders.
Project structure:
main.rs
school/mod.rs
school/student.rs
Output:
Here:
main.rsdeclares theschoolmodule.school/mod.rsdeclares thestudentsubmodule.school/student.rscontains the student-related function.
Note: Modern Rust also supports school.rs instead of school/mod.rs for declaring the parent module. Both approaches are valid.
9. What Are Packages and Crates?
Rust has three important concepts: Package, Crate, and Module.
Package
A package is a project managed by Cargo.
It contains a Cargo.toml file and one or more crates.
Crate
A crate is the unit of compilation in Rust.
There are two main types:
- Binary crate – An executable application.
- Library crate – A library that other code can use.
Module
A module organizes code inside a crate.
Comparison:
Concept | Purpose |
|---|---|
Package | Manages a Rust project |
Crate | Compilation unit |
Module | Organizes code inside a crate |
Common crate root files
File | Purpose |
|---|---|
| Root of a binary crate |
| Root of a library crate |
A package can contain both a binary crate and a library crate.
10. Using as to Rename Imports
You can rename an imported item using the as keyword.
Example:
Output:
Here, show_name is imported with the new name display_name.
11. Using pub use to Re-export Items
Sometimes you want to make an item available through another module path.
Rust provides pub use for this purpose.
Example:
Output:
Here, pub use re-exports the function so it can be accessed through the current module's public interface.
12. Common Mistakes
Mistake | Explanation |
|---|---|
Forgetting | Rust does not automatically include the separate module file |
Forgetting | An item may not be accessible from outside its module |
Using the wrong path | Check the module hierarchy |
Confusing |
|
Incorrect file structure | Module file names and folder locations must match the declarations |