What if you could organize many things at once without writing each one down separately?
Why Array Declaration and Initialization in DSA Rust?
Imagine you have a row of empty boxes and you want to put your favorite toys in them one by one, but you have to write down each toy's name on a separate piece of paper every time you add one.
Writing down each toy's name separately is slow and easy to mess up. If you forget a box or write the wrong name, it becomes hard to find your toys later.
Using arrays is like having a labeled shelf where you can quickly place and find your toys by their position, all organized neatly from the start.
let toy1 = "Car"; let toy2 = "Doll"; let toy3 = "Ball";
let toys = ["Car", "Doll", "Ball"];
Arrays let you store many items together in order, making it easy to access, change, or count them quickly.
Think of a row of mailboxes where each box holds letters for a specific house number; arrays help you organize and find letters by their box number fast.
Arrays group multiple values in one place.
They keep items in order for easy access.
Declaration and initialization set up this organized storage.
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
