Writing first Rust program - Time & Space Complexity
When writing your first Rust program, it is helpful to understand how the program's steps grow as you add more instructions or data.
We want to see how the time to run changes when the program gets bigger or more complex.
Analyze the time complexity of the following code snippet.
fn main() {
println!("Hello, world!");
}
This code prints a simple greeting message once to the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing one message once.
- How many times: Exactly one time.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 print operation |
| 10 | 1 print operation (still the same) |
| 100 | 1 print operation (no change) |
Pattern observation: The number of operations stays the same no matter how big the input is because the program only prints once.
Time Complexity: O(1)
This means the program takes the same amount of time to run no matter how big the input or program size is.
[X] Wrong: "Printing a message once takes longer if the program is bigger."
[OK] Correct: The program only prints once, so the time does not grow with input size here.
Understanding simple time complexity like this builds your confidence to analyze bigger programs later. It shows you how to think about how code runs as it grows.
"What if we added a loop to print the message n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand Rust program entry point
Rust programs always start execution from themainfunction.Step 2: Identify the correct function name
The correct function to start a Rust program isfn main().Final Answer:
By defining afn main()function -> Option AQuick Check:
Rust program entry point = fn main() [OK]
- Using incorrect function names like start() or begin()
- Omitting the main function entirely
- Confusing Rust with other languages' entry points
Solution
Step 1: Identify Rust's print macro
Rust uses the macroprintln!to print text with a newline.Step 2: Compare options
Onlyprintln!("Hello, world!");is valid Rust syntax for printing with a newline.Final Answer:
println!("Hello, world!"); -> Option BQuick Check:
Print with newline = println! macro [OK]
- Using print! without newline when newline is expected
- Using functions from other languages like echo or console.log
- Missing the exclamation mark after println
fn main() {
println!("{}", 5 + 3);
}Solution
Step 1: Understand the println! macro with placeholders
The{}inside the string is a placeholder for the value following the string.Step 2: Calculate the expression inside println!
The expression5 + 3evaluates to 8, which replaces the{}.Final Answer:
8 -> Option AQuick Check:
5 + 3 = 8 printed [OK]
- Expecting the literal string '5 + 3' to print
- Confusing {} as a string literal
- Thinking it causes a compile error
fn main() {
println!("Hello, world!")
}Solution
Step 1: Check syntax of println! macro call
In Rust, statements must end with a semicolon unless they are the last expression in a block.Step 2: Identify missing semicolon
Theprintln!("Hello, world!")line is missing a semicolon at the end.Final Answer:
Missing semicolon after println! macro -> Option CQuick Check:
Statements need semicolon unless last expression [OK]
- Forgetting semicolon after macro calls
- Thinking main function name is wrong
- Assuming println! is not a macro
Solution
Step 1: Understand variable interpolation in println!
Rust uses{}as a placeholder and variables are passed after the string.Step 2: Check each option for correct syntax and output
fn main() { let name = "Alice"; println!("Hello, {}!", name); } correctly usesprintln!("Hello, {}!", name);to print the variable's value.Final Answer:
fn main() { let name = "Alice"; println!("Hello, {}!", name); } -> Option DQuick Check:
Use {} and pass variable to println! [OK]
- Putting variable name inside quotes without {}
- Using print! instead of println! (missing newline)
- Trying to use {name} without Rust 1.58+ raw identifiers
Ready to prove it?
Pass the test and this pattern moves unknown
