Array Declaration and Initialization in DSA Rust - Time & Space Complexity
We want to understand how long it takes to create and fill an array in Rust.
How does the time needed grow when the array size grows?
Analyze the time complexity of the following code snippet.
fn main() {
let size = 5;
let mut arr = [0; 5];
for i in 0..size {
arr[i] = i * 2;
}
println!("{:?}", arr);
}
This code creates an array of 5 integers and fills it with values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that assigns values to each array element.
- How many times: It runs exactly once for each element, so 5 times here, or n times in general.
As the array size grows, the number of assignments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with the size of the array.
Time Complexity: O(n)
This means the time to fill the array grows in a straight line with the number of elements.
[X] Wrong: "Creating and initializing an array is instant and does not depend on size."
[OK] Correct: Each element must be set, so the time grows with the number of elements.
Knowing how array initialization scales helps you understand basic data structure setup, a key skill in coding interviews.
"What if we used a vector and pushed elements one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand Rust array syntax
Rust arrays require a fixed size and type, declared as [type; size].Step 2: Check initialization correctness
[0; 5] means 5 elements all set to 0, which matches the declaration.Final Answer:
let arr: [i32; 5] = [0; 5]; -> Option BQuick Check:
Correct Rust array declaration = let arr: [i32; 5] = [0; 5]; [OK]
- Missing size in array type declaration
- Using wrong syntax like [0, 5] for repeated values
- Declaring array without fixed size
Solution
Step 1: Identify mutable variable syntax in Rust
Mutable variables use 'let mut' before the variable name.Step 2: Confirm array declaration and initialization
Array type is [f64; 3] and initialized with [1.0; 3], which is correct.Final Answer:
let mut arr: [f64; 3] = [1.0; 3]; -> Option AQuick Check:
Mutable array syntax = let mut arr: [f64; 3] = [1.0; 3]; [OK]
- Placing 'mut' after variable name
- Using 'mut let' instead of 'let mut'
- Incorrect array type syntax
let arr = [2; 4];
println!("{:?}", arr);Solution
Step 1: Understand array initialization [2; 4]
This creates an array with 4 elements, each set to 2.Step 2: Print array with debug format
Using {:?} prints the array elements as a list.Final Answer:
[2, 2, 2, 2] -> Option CQuick Check:
Array with repeated values prints as list [OK]
- Confusing size and value positions
- Expecting syntax [2;4] to print literally
- Assuming compilation error due to syntax
let arr: [i32; 3] = [1, 2];
Solution
Step 1: Check declared array size
Array is declared with size 3 but initialized with only 2 elements.Step 2: Understand Rust array size rules
Rust requires the number of elements to match the declared size exactly.Final Answer:
Array size mismatch error -> Option AQuick Check:
Declared size must match initializer length [OK]
- Ignoring size mismatch
- Assuming Rust allows shorter initializers
- Confusing type mismatch with size error
Solution
Step 1: Understand 2D array type declaration
A 3x3 array of i32 is declared as [[i32; 3]; 3], meaning 3 arrays each of size 3.Step 2: Check initialization syntax
[[7; 3]; 3] creates 3 arrays each filled with 3 sevens, forming a 3x3 matrix.Final Answer:
let matrix: [[i32; 3]; 3] = [[7; 3]; 3]; -> Option DQuick Check:
Nested arrays use [[type; cols]; rows] [OK]
- Incorrect type syntax like [i32; 3][3]
- Initializing flat array instead of nested
- Mismatch between declared size and initializer
Ready to prove it?
Pass the test and this pattern moves unknown
