Bird
Raised Fist0
Rustprogramming~10 mins

Module visibility in Rust - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does the pub keyword do in Rust module visibility?
easy
A. Makes a function or item accessible outside its module
B. Makes a function private to its module
C. Deletes the function from the module
D. Renames the function for external use

Solution

  1. Step 1: Understand the role of pub

    The pub keyword in Rust is used to make functions, structs, or other items accessible from outside the module they are defined in.
  2. Step 2: Contrast with default visibility

    By default, items are private and cannot be accessed outside their module unless marked with pub.
  3. Final Answer:

    Makes a function or item accessible outside its module -> Option A
  4. Quick Check:

    pub = public access [OK]
Hint: Remember: pub means public, so others can use it [OK]
Common Mistakes:
  • Thinking pub hides the item
  • Confusing pub with renaming
  • Assuming pub deletes the item
2. Which of the following is the correct way to make a struct public in Rust?
easy
A. struct MyStruct { pub field: i32 }
B. struct pub MyStruct { field: i32 }
C. pub struct MyStruct { field: i32 }
D. struct MyStruct pub { field: i32 }

Solution

  1. Step 1: Recall correct syntax for public structs

    In Rust, to make a struct public, the pub keyword must come before the struct keyword.
  2. Step 2: Check placements of pub

    pub struct MyStruct { field: i32 } correctly places pub before struct. Placing pub inside the braces (on field), writing struct pub MyStruct, or struct MyStruct pub {} are incorrect.
  3. Final Answer:

    pub struct MyStruct { field: i32 } -> Option C
  4. Quick Check:

    pub struct is correct syntax [OK]
Hint: pub goes before struct, not inside or after [OK]
Common Mistakes:
  • Placing pub inside struct braces
  • Putting pub after struct name
  • Writing pub in the wrong order
3. Given the code:
pub mod outer {
    pub mod inner {
        pub fn greet() { println!("Hello!"); }
    }
}

fn main() {
    outer::inner::greet();
}

What will be the output when this code runs?
medium
A. No output
B. Error: greet is private
C. Error: inner module is private
D. Hello!

Solution

  1. Step 1: Check visibility of modules and function

    The outer module is public, containing pub mod inner with pub fn greet(), making them all accessible from main.
  2. Step 2: Confirm function call and output

    The call outer::inner::greet() is valid and will print "Hello!" to the console.
  3. Final Answer:

    Hello! -> Option D
  4. Quick Check:

    pub modules and functions allow access [OK]
Hint: pub on modules and functions means accessible outside [OK]
Common Mistakes:
  • Overlooking that the outer module needs pub
  • Thinking greet is private by default
  • Ignoring nested module visibility
4. What is wrong with this Rust code?
mod my_mod {
    fn secret() {
        println!("Secret function");
    }
}

fn main() {
    my_mod::secret();
}
medium
A. secret function is private and cannot be called from main
B. Missing semicolon after println!
C. Module my_mod is not declared public
D. Function secret must have pub keyword inside main

Solution

  1. Step 1: Check function visibility

    The function secret is not marked pub, so it is private to my_mod.
  2. Step 2: Understand call from main

    Since main is outside my_mod, it cannot call a private function, causing a compile error.
  3. Final Answer:

    secret function is private and cannot be called from main -> Option A
  4. Quick Check:

    Private functions can't be accessed outside module [OK]
Hint: Private functions can't be called outside their module [OK]
Common Mistakes:
  • Thinking module must be pub to call function
  • Assuming missing semicolon causes error
  • Believing pub inside main is needed
5. You have a private struct Config inside a module, but want to allow other modules to create instances with limited access to its fields. Which visibility setup is correct?
hard
A. Make struct private and fields public
B. Make struct pub but keep fields private
C. Make both struct and fields private
D. Keep struct private and make fields pub

Solution

  1. Step 1: Understand struct and field visibility

    Making the struct pub allows other modules to create instances, but keeping fields private restricts direct access to internal data.
  2. Step 2: Evaluate other approaches

    Keeping the struct private (with or without pub fields) prevents other modules from constructing instances. Making everything private blocks access entirely.
  3. Final Answer:

    Make struct pub but keep fields private -> Option B
  4. Quick Check:

    pub struct + private fields = controlled access [OK]
Hint: pub struct with private fields controls access well [OK]
Common Mistakes:
  • Making fields pub but struct private (no instance creation)
  • Making everything private (no access)
  • Exposing fields unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard