0
0
Rustprogramming~10 mins

Why modules are used in Rust - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why modules are used
Start Program
Define Module
Add Functions/Variables
Use Module in Main
Run Program
Organized Code & Avoid Name Conflicts
Modules group related code together, keep code organized, and prevent name conflicts.
Execution Sample
Rust
mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    let sum = math::add(2, 3);
    println!("Sum is {}", sum);
}
This code defines a module 'math' with a function 'add' and uses it in main to add two numbers.
Execution Table
StepActionEvaluationResult
1Define module 'math'Module createdmath module ready
2Define function 'add' inside 'math'Function availableadd(a,b) adds two numbers
3Call math::add(2, 3) in mainFunction called with a=2, b=3Returns 5
4Assign result to 'sum'sum = 5sum holds value 5
5Print 'Sum is 5'Output to consoleSum is 5
💡 Program ends after printing the sum.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
sumundefined555
Key Moments - 3 Insights
Why do we write 'pub' before the function inside the module?
Without 'pub', the function 'add' is private and cannot be used outside the module, as shown in step 3 where 'math::add' is called.
What happens if we try to call 'add' without the module prefix?
The code will not compile because 'add' is inside 'math' module and must be accessed with 'math::add', as seen in step 3.
Why use modules instead of putting all code in main?
Modules keep code organized and avoid name conflicts, making large programs easier to manage, as summarized in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'sum' after step 4?
A5
Bundefined
C2
D3
💡 Hint
Check the 'variable_tracker' table under 'After Step 4' for 'sum'.
At which step is the function 'add' called?
AStep 5
BStep 1
CStep 3
DStep 2
💡 Hint
Look at the 'execution_table' under 'Action' for function call.
If we remove 'pub' from the function, what will happen at step 3?
AFunction call works normally
BCompilation error due to private function
CProgram prints wrong result
DFunction is called but returns zero
💡 Hint
Refer to 'key_moments' about 'pub' keyword and step 3 in 'execution_table'.
Concept Snapshot
Modules group related code.
Use 'mod' to define modules.
Use 'pub' to make items accessible outside.
Access with module_name::item_name.
Keeps code organized and avoids name conflicts.
Full Transcript
Modules in Rust help organize code by grouping related functions and variables together. We define a module using 'mod' and make functions public with 'pub' so they can be used outside the module. In the example, the 'math' module has an 'add' function. The main function calls 'math::add' to add two numbers. This keeps code clean and prevents name conflicts. The execution steps show defining the module, calling the function, storing the result, and printing it. Variables like 'sum' change value as the program runs. Beginners often wonder why 'pub' is needed and why the module prefix is required. Removing 'pub' causes errors because the function is private. Using modules is like putting tools in labeled boxes so you can find and use them easily without mixing things up.