Defining modules in Rust - Time & Space Complexity
When we define modules in Rust, we organize code into parts. Understanding time complexity here means seeing how the program's work grows when using modules.
We want to know: does adding modules change how long the program takes to run?
Analyze the time complexity of the following code snippet.
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
fn main() {
let result = math::add(5, 3);
println!("{}", result);
}
This code defines a module named math with a function add. The main function calls this add function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single function call to
addinside the module. - How many times: Exactly once in this example.
Since the module just holds a simple function, the work done does not grow with input size here.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 function call |
| 100 | 1 function call |
| 1000 | 1 function call |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is, because the module just groups code without adding loops or repeated work.
[X] Wrong: "Using modules makes the program slower because it adds extra steps."
[OK] Correct: Modules only organize code and do not add repeated work or loops by themselves, so they don't slow down the program.
Knowing that modules organize code without changing how much work the program does helps you explain clean code design and performance clearly in interviews.
"What if the module contained a loop inside the function? How would the time complexity change?"
Practice
Solution
Step 1: Understand Rust module declaration
Rust uses the keywordmodto declare a module inside a file.Step 2: Differentiate from other keywords
useimports modules,pubmakes items public, andmoduleis not a Rust keyword.Final Answer:
mod -> Option CQuick Check:
Keyword for defining modules = mod [OK]
- Using 'module' instead of 'mod'
- Confusing 'use' with 'mod'
- Forgetting 'mod' keyword
network in Rust?Solution
Step 1: Recall correct module syntax
The correct syntax to declare a module ismod name { }without parentheses.Step 2: Check each option
mod network {} usesmod network {}which is correct. module network {} uses 'module' which is invalid. pub mod network() {} adds parentheses which are not allowed. use network {} uses 'use' which is for importing.Final Answer:
mod network {} -> Option DQuick Check:
Module declaration = mod name {} [OK]
- Adding parentheses after module name
- Using 'module' instead of 'mod'
- Confusing 'use' with 'mod'
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
fn main() {
println!("{}", math::add(3, 4));
}Solution
Step 1: Check module and function visibility
The functionaddis markedpub, so it can be accessed outside the module.Step 2: Evaluate the function call
Callingmath::add(3, 4)returns 3 + 4 = 7, which is printed.Final Answer:
7 -> Option AQuick Check:
Public function call returns sum = 7 [OK]
- Forgetting 'pub' causes compilation error
- Expecting function name printed
- Confusing output with zero
mod utils {
fn helper() {
println!("Helper function");
}
}
fn main() {
utils::helper();
}Solution
Step 1: Check function visibility
The functionhelperis not markedpub, so it is private to the module.Step 2: Understand access from main
Trying to callutils::helper()from outside causes a compilation error due to privacy.Final Answer:
Function 'helper' is private and cannot be accessed outside the module -> Option AQuick Check:
Private functions can't be called outside module [OK]
- Assuming all functions are public by default
- Ignoring privacy rules
- Thinking modules block all calls
main.rs and network.rs. How do you correctly declare and use the network module in main.rs?Solution
Step 1: Understand module file linking
To link an external file as a module, usemod network;inmain.rs. This tells Rust to loadnetwork.rs.Step 2: Make functions public in
Functions insidenetwork.rsnetwork.rsmust be markedpubto be accessible frommain.rs.Final Answer:
Write mod network; in main.rs and make functions in network.rs public with pub -> Option BQuick Check:
Use mod filename; and pub functions for multi-file modules [OK]
- Using 'use' instead of 'mod' to declare modules
- Declaring mod inside the module file
- Using extern crate for local modules
Ready to prove it?
Pass the test and this pattern moves unknown
