0
0
Rustprogramming~10 mins

Defining modules in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining modules
Start main.rs
Declare module with mod
Rust compiler looks for module file or inline code
Module code is compiled
Use module items with use or fully qualified path
Program runs using module's functions/variables
This flow shows how Rust finds and compiles modules declared with mod, then how the main code uses them.
Execution Sample
Rust
mod greetings {
    pub fn hello() {
        println!("Hello from module!");
    }
}

fn main() {
    greetings::hello();
}
Defines a module greetings with a public function hello, then calls it from main.
Execution Table
StepActionCode EvaluatedResult/Output
1Declare module greetingsmod greetings { ... }Module greetings is registered
2Define public function hello inside greetingspub fn hello() { println!(...) }Function hello is available publicly
3Start main functionfn main() { ... }Program entry point
4Call greetings::hello()greetings::hello();Prints: Hello from module!
5Program ends}Program terminates normally
💡 Program ends after main finishes execution
Variable Tracker
VariableStartAfter greetings::hello() callFinal
No variablesN/AN/AN/A
Key Moments - 2 Insights
Why do we need to use pub before the function hello?
Without pub, the function hello is private to the module and cannot be called from main. See execution_table step 2 and 4 where hello is declared public and then called.
What does mod greetings mean in the code?
It tells Rust to create a module named greetings. The compiler looks for code inside the braces or in greetings.rs file. See execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AThe program prints 'Hello from module!'
BThe module greetings is declared
CThe program ends
DThe function hello is defined
💡 Hint
Check the 'Result/Output' column at step 4 in execution_table
At which step does the program start running main()?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for when main() starts in execution_table
If we remove pub from the function hello, what will happen when calling greetings::hello()?
AIt will print the message anyway
BRuntime error when calling hello
CCompilation error because hello is private
DThe program will ignore the call
💡 Hint
Refer to key_moments about pub keyword and execution_table step 4
Concept Snapshot
Defining modules in Rust:
- Use mod keyword to declare a module
- Module code can be inline or in separate file
- Use pub to make functions accessible outside module
- Call module items with module_name::item_name
- Modules help organize code into namespaces
Full Transcript
This example shows how to define a module in Rust using the mod keyword. The module greetings contains a public function hello. The main function calls greetings::hello() which prints a message. The pub keyword is important to allow access to the function from outside the module. The execution steps show module declaration, function definition, main start, function call, and program end. Variables are not used here. Common confusions include why pub is needed and what mod means. The quiz tests understanding of these steps and concepts.