0
0
Rustprogramming~10 mins

Module visibility in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Module visibility
Define module
Declare items: functions, structs
Set visibility: pub or private
Access items from outside module
Compiler checks visibility
Allowed
Code runs
This flow shows how Rust modules define items with visibility, and how the compiler checks access from outside the module, allowing or blocking usage.
Execution Sample
Rust
mod my_mod {
    pub fn public_func() {
        println!("Hello from public function");
    }
    fn private_func() {
        println!("Hello from private function");
    }
}

fn main() {
    my_mod::public_func();
    // my_mod::private_func(); // Error: private function
}
This code defines a module with a public and a private function, then calls the public one from main, showing visibility in action.
Execution Table
StepCode LineActionVisibility CheckResult
1mod my_mod { ... }Define module with two functionsN/AModule created
2pub fn public_func()Declare public functionMarked as publicAccessible outside module
3fn private_func()Declare private functionDefault privateNot accessible outside module
4my_mod::public_func();Call public function from mainAllowedPrints: Hello from public function
5// my_mod::private_func();Call private function from mainDeniedCompilation error: private function
💡 Execution stops because calling private_func from outside module is not allowed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
my_mod::public_funcundefinedfunction defined (public)unchangedcalled successfullyunchanged
my_mod::private_funcundefinedundefinedfunction defined (private)undefinedattempted call (error)
Key Moments - 2 Insights
Why can we call public_func but not private_func from main?
Because public_func is declared with pub, making it accessible outside the module, while private_func is private by default and cannot be accessed from outside. See execution_table rows 2, 3, 4, and 5.
What happens if we remove pub from public_func?
Then public_func becomes private, so calling it from main will cause a compilation error, similar to private_func. This is shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the visibility of private_func after step 3?
AUndefined
BPublic
CPrivate
DProtected
💡 Hint
Check execution_table row 3 under Visibility Check column
At which step does the program print 'Hello from public function'?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 under Result column
If we remove pub from public_func, what will happen at step 4?
AIt will print the message
BCompilation error due to private function
CRuntime error
DNothing happens
💡 Hint
Refer to key_moments explanation about removing pub and execution_table row 5
Concept Snapshot
Rust modules group code items.
By default, items are private.
Use pub keyword to make items public.
Public items can be accessed outside the module.
Private items cause compile errors if accessed externally.
Full Transcript
This visual execution shows how Rust modules control visibility of functions. We define a module with two functions: one public and one private. The public function is marked with pub, so it can be called from outside the module, like in main. The private function has no pub, so it is hidden outside the module. When main calls the public function, it runs and prints a message. But calling the private function causes a compile error because it is not visible. This teaches how Rust enforces module visibility rules at compile time.