Why modules are used in Rust - Performance Analysis
We want to see how using modules affects the time it takes for a Rust program to run.
Does organizing code into modules change how fast the program works?
Analyze the time complexity of the following code snippet.
mod math_utils {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
fn main() {
let result = math_utils::add(5, 7);
println!("Result: {}", result);
}
This code defines a module with a simple add function and calls it from main.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single function call inside main.
- How many times: Exactly once, no loops or recursion.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 function call |
| 100 | Still 1 function call |
| 1000 | Still 1 function call |
Pattern observation: The number of operations does not grow with input size here.
Time Complexity: O(1)
This means the program runs in constant time regardless of input size because modules just organize code.
[X] Wrong: "Using modules makes the program slower because it adds extra steps."
[OK] Correct: Modules only help organize code and do not add repeated work or slow down execution by themselves.
Understanding that modules help keep code clean without slowing it down shows you know how to write good, maintainable programs.
"What if the module contained a loop that ran n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of modules
Modules help break down code into smaller parts that are easier to manage and understand.Step 2: Compare other options
Options A, B, and D do not describe the main reason for using modules; they are unrelated to code organization.Final Answer:
To organize code into smaller, manageable parts -> Option AQuick Check:
Modules organize code = C [OK]
- Thinking modules speed up the program
- Believing modules increase file size
- Confusing modules with comments
math in Rust?Solution
Step 1: Recall Rust module syntax
Rust uses the keywordmodto declare a module.Step 2: Check other options
Options B, C, and D use incorrect keywords not valid in Rust for modules.Final Answer:
mod math { } -> Option BQuick Check:
Usemodto declare modules = A [OK]
- Using 'module' instead of 'mod'
- Confusing 'use' with module declaration
- Trying 'package' keyword like other languages
mod greetings {
pub fn hello() {
println!("Hello from module!");
}
}
fn main() {
greetings::hello();
}Solution
Step 1: Check function visibility
The functionhellois markedpub, so it is accessible outside the module.Step 2: Understand function call
The main function callsgreetings::hello(), which prints the message.Final Answer:
Hello from module! -> Option CQuick Check:
Public function call prints message = D [OK]
- Forgetting to use 'pub' makes function private
- Assuming no output without checking visibility
- Thinking module must be imported separately
mod tools {
fn hammer() {
println!("Hammering");
}
}
fn main() {
tools::hammer();
}Solution
Step 1: Check function visibility
The functionhammeris not markedpub, so it is private to the module.Step 2: Understand access rules
Trying to calltools::hammer()frommaincauses an error because the function is private.Final Answer:
Function hammer is private and cannot be accessed outside the module -> Option AQuick Check:
Private function access error = A [OK]
- Assuming all module functions are public by default
- Thinking module declaration is wrong
- Missing semicolon in println! (actually correct here)
math and utils. The math module has a public function add that you want to use in utils. How should you structure the code to allow utils to call math::add?Solution
Step 1: Make function public
Theaddfunction must be markedpubto be accessible outsidemath.Step 2: Use 'use' to import function
Insideutils, useuse crate::math::add;to bringaddinto scope for calling.Final Answer:
Declare both modules in the root, markaddaspub, and useuse crate::math::add;insideutils-> Option DQuick Check:
Public function + use import = B [OK]
- Leaving function private and expecting access
- Trying to call private functions directly
- Thinking modules cannot share code
Ready to prove it?
Pass the test and this pattern moves unknown
