Constants in Rust - Time & Space Complexity
When using constants in Rust, it's important to see how they affect the speed of a program.
We want to know how the program's running time changes as input grows when constants are involved.
Analyze the time complexity of the following code snippet.
const MAX: usize = 1000;
fn print_numbers() {
for i in 0..MAX {
println!("{}", i);
}
}
This code prints numbers from 0 up to a fixed constant MAX.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A for-loop running from 0 to MAX.
- How many times: Exactly MAX times, which is a fixed number.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1000 (constant) |
| 100 | 1000 (constant) |
| 1000 | 1000 (constant) |
Pattern observation: The number of operations stays the same no matter how input changes because MAX is fixed.
Time Complexity: O(1)
This means the running time does not grow with input size; it stays constant.
[X] Wrong: "Since there is a loop, the time must grow with input size."
[OK] Correct: The loop runs a fixed number of times set by a constant, so it does not depend on input size.
Understanding constants helps you explain why some parts of code run quickly no matter the input size, a useful skill in interviews.
"What if MAX was a variable input instead of a constant? How would the time complexity change?"
Practice
Solution
Step 1: Understand constant properties in Rust
Constants in Rust must have an explicit type and are immutable by design.Step 2: Evaluate each option
Constants must have a type and cannot be changed after declaration correctly states constants require a type and cannot change. The other options are incorrect because constants cannot be changed anytime during execution, they must have a type annotation, and they can be declared outside functions.Final Answer:
Constants must have a type and cannot be changed after declaration. -> Option BQuick Check:
Constants are immutable with explicit types = B [OK]
- Thinking constants can be changed like variables
- Omitting the type annotation for constants
- Assuming constants are only for inside functions
MAX_POINTS with value 100 in Rust?Solution
Step 1: Recall Rust constant syntax
Constants use the keywordconst, require a type annotation, and use an equals sign to assign a value.Step 2: Check each option
const MAX_POINTS: i32 = 100; matches the correct syntax:const MAX_POINTS: i32 = 100;.const MAX_POINTS = 100;misses the type,var MAX_POINTS: i32 = 100;uses invalid keywordvar,let MAX_POINTS: i32 = 100;usesletfor variables.Final Answer:
const MAX_POINTS: i32 = 100; -> Option DQuick Check:
Constants needconst+ type + value = D [OK]
- Forgetting the type annotation
- Using let or var instead of const
- Missing the semicolon at the end
const PI: f64 = 3.14;
fn main() {
println!("Value of PI is {}", PI);
}Solution
Step 1: Understand constant declaration and usage
The constantPIis declared with typef64and value3.14. It is used insideprintln!macro.Step 2: Predict output of the print statement
The print statement will replace{}with the value ofPI, which is3.14. No errors occur.Final Answer:
Value of PI is 3.14 -> Option AQuick Check:
Printing constant value outputs its assigned number = A [OK]
- Expecting the name 'PI' to print instead of its value
- Thinking missing type causes error here (type is given)
- Confusing runtime errors with compile-time constants
const MAX: i32 = 10;
fn main() {
MAX = 20;
println!("{}", MAX);
}Solution
Step 1: Analyze constant assignment
Constants in Rust are immutable and cannot be assigned a new value after declaration.Step 2: Check the code for errors
The lineMAX = 20;tries to change the constant, which causes a compile-time error.Final Answer:
Cannot assign a new value to a constant after declaration. -> Option CQuick Check:
Constants are immutable; reassignment causes error = C [OK]
- Trying to change a constant's value
- Thinking constants behave like variables
- Ignoring compiler error messages about immutability
Solution
Step 1: Check constant declaration correctness
const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } declares a constant with typeu32and value 7, which is correct syntax.Step 2: Verify usage in function
The function multiplies weeks by the constant, correctly calculating total days. let DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } usesletwhich is not a constant. const DAYS_IN_WEEK = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } misses the type annotation. static DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks + DAYS_IN_WEEK } usesstaticand adds weeks and days, which is incorrect logic.Final Answer:
const DAYS_IN_WEEK: u32 = 7; fn total_days(weeks: u32) -> u32 { weeks * DAYS_IN_WEEK } -> Option AQuick Check:
Correct const declaration and usage in calculation = A [OK]
- Using let instead of const for fixed values
- Omitting type annotation in const declaration
- Using addition instead of multiplication for total days
Ready to prove it?
Pass the test and this pattern moves unknown
