Module visibility in Rust - Time & Space Complexity
When working with module visibility in Rust, it's important to understand how access to code parts affects program behavior.
We want to see how the visibility rules impact the number of operations or checks done as the code grows.
Analyze the time complexity of the following code snippet.
mod outer {
pub mod inner {
pub fn greet() {
println!("Hello from inner module!");
}
}
}
fn main() {
outer::inner::greet();
}
This code defines nested modules with public visibility and calls a function from the inner module.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or recursion here; the main operation is a single function call through module paths.
- How many times: The function is called once in this example.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The number of operations grows linearly with the number of function calls, regardless of module visibility.
Time Complexity: O(n)
This means the time to run grows directly with how many times you call functions through modules.
[X] Wrong: "Making modules public slows down the program a lot because of extra checks."
[OK] Correct: Visibility controls are checked at compile time, so they don't add extra work when the program runs.
Understanding how module visibility affects program structure helps you write clear and maintainable code, a skill valued in many coding tasks.
"What if we changed the inner module's visibility from public to private? How would the time complexity change when calling its functions?"
Practice
pub keyword do in Rust module visibility?Solution
Step 1: Understand the role of
Thepubpubkeyword in Rust is used to make functions, structs, or other items accessible from outside the module they are defined in.Step 2: Contrast with default visibility
By default, items are private and cannot be accessed outside their module unless marked withpub.Final Answer:
Makes a function or item accessible outside its module -> Option AQuick Check:
pub= public access [OK]
- Thinking pub hides the item
- Confusing pub with renaming
- Assuming pub deletes the item
Solution
Step 1: Recall correct syntax for public structs
In Rust, to make a struct public, thepubkeyword must come before thestructkeyword.Step 2: Check placements of pub
pub struct MyStruct { field: i32 }correctly placespubbeforestruct. Placingpubinside the braces (on field), writingstruct pub MyStruct, orstruct MyStruct pub {}are incorrect.Final Answer:
pub struct MyStruct { field: i32 } -> Option CQuick Check:
pub structis correct syntax [OK]
- Placing pub inside struct braces
- Putting pub after struct name
- Writing pub in the wrong order
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?
Solution
Step 1: Check visibility of modules and function
Theoutermodule is public, containingpub mod innerwithpub fn greet(), making them all accessible frommain.Step 2: Confirm function call and output
The callouter::inner::greet()is valid and will print "Hello!" to the console.Final Answer:
Hello! -> Option DQuick Check:
pub modules and functions allow access [OK]
- Overlooking that the outer module needs pub
- Thinking greet is private by default
- Ignoring nested module visibility
mod my_mod {
fn secret() {
println!("Secret function");
}
}
fn main() {
my_mod::secret();
}Solution
Step 1: Check function visibility
The functionsecretis not markedpub, so it is private tomy_mod.Step 2: Understand call from main
Sincemainis outsidemy_mod, it cannot call a private function, causing a compile error.Final Answer:
secret function is private and cannot be called from main -> Option AQuick Check:
Private functions can't be accessed outside module [OK]
- Thinking module must be pub to call function
- Assuming missing semicolon causes error
- Believing pub inside main is needed
Config inside a module, but want to allow other modules to create instances with limited access to its fields. Which visibility setup is correct?Solution
Step 1: Understand struct and field visibility
Making the structpuballows other modules to create instances, but keeping fields private restricts direct access to internal data.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.Final Answer:
Make structpubbut keep fields private -> Option BQuick Check:
pub struct + private fields = controlled access [OK]
- Making fields pub but struct private (no instance creation)
- Making everything private (no access)
- Exposing fields unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
