Program structure in Rust - Time & Space Complexity
When we look at how a Rust program is built, we want to know how the time it takes to run changes as the program grows.
We ask: How does the program's structure affect how long it runs?
Analyze the time complexity of the following code snippet.
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
for num in &numbers {
println!("Number: {}", num);
}
}
This code creates a list of numbers and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list to print each number.
- How many times: Once for each number in the list.
As the list gets bigger, the program prints more numbers, so it takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The time grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items.
[X] Wrong: "The program runs in the same time no matter how many numbers there are."
[OK] Correct: Because the program prints each number, more numbers mean more work and more time.
Understanding how loops affect time helps you explain how your code will behave as data grows, a key skill in programming.
"What if we added another loop inside the first one to print pairs of numbers? How would the time complexity change?"
Practice
Solution
Step 1: Understand Rust program entry point
Rust programs always begin execution from themainfunction.Step 2: Check other options
Code outside functions or other function names likestartare not entry points.Final Answer:
Inside themainfunction -> Option BQuick Check:
Rust starts at main() [OK]
- Thinking code outside main() runs first
- Assuming a function named start() runs automatically
- Believing execution starts at the last line
Solution
Step 1: Identify Rust function syntax
Rust functions start withfn, followed by the name and parentheses.Step 2: Check each option
Only fn main() { println!("Hello, world!"); } usesfn main()correctly; others use wrong keywords.Final Answer:
fn main() { println!("Hello, world!"); } -> Option CQuick Check:
Rust functions start with fn [OK]
- Using function or def instead of fn
- Omitting fn keyword
- Missing parentheses after function name
fn main() {
println!("Start");
{
println!("Inside block");
}
println!("End");
}Solution
Step 1: Follow the code execution order
The program prints "Start", then enters the block and prints "Inside block", then prints "End" after the block.Step 2: Confirm output sequence
The output lines appear in the order: Start, Inside block, End.Final Answer:
Start\nInside block\nEnd -> Option AQuick Check:
Code runs top to bottom inside blocks [OK]
- Assuming block runs after all code
- Mixing order of print statements
- Ignoring braces as code grouping
fn main() {
println!("Hello");
// Missing closing brace
Solution
Step 1: Check code structure
The main function starts withfn main() {but never closes with a}.Step 2: Verify other syntax
The semicolon after println! is present, comment syntax is correct, and println! is spelled correctly.Final Answer:
Missing closing brace '}' for main function -> Option DQuick Check:
Every { needs a matching } [OK]
- Forgetting to close function with }
- Confusing comment syntax with errors
- Missing semicolon errors instead of braces
main function?Solution
Step 1: Identify Rust comment syntax
Rust uses//for single-line comments and/* */for block comments.Step 2: Check each option
Only// This prints Hello println!("Hello");uses Rust's single-line comment syntax correctly;/* ... */is a block comment,#is Python-style, and<!-- ... -->is HTML.Final Answer:
// This prints Hello println!("Hello"); -> Option AQuick Check:
Single-line comments start with // [OK]
- Using # or for comments
- Confusing block comments with single-line
- Placing comments inside println! macro
Ready to prove it?
Pass the test and this pattern moves unknown
