An array in Rust is a fixed-size collection of elements of the same type stored in contiguous memory. It has a known size at compile time.
Click to reveal answer
beginner
How do you declare an array of 5 integers initialized to zero in Rust?
You write: <code>let arr: [i32; 5] = [0; 5];</code> This creates an array of 5 integers, all set to 0.
Click to reveal answer
beginner
How do you access the third element of an array named arr?
You use index 2 because Rust arrays are zero-indexed: arr[2] gives the third element.
Click to reveal answer
intermediate
What happens if you try to access an array element out of its bounds in Rust?
Rust will panic at runtime and stop the program because it checks array bounds to prevent errors.
Click to reveal answer
beginner
How can you find the length of an array arr in Rust?
Use arr.len() to get the number of elements in the array.
Click to reveal answer
How do you declare an array of 3 strings in Rust?
Alet arr: [&str; 3] = ["a", "b", "c"];
Blet arr = ["a", "b", "c"];
Clet arr: [String; 3] = ["a", "b", "c"];
Dlet arr = ("a", "b", "c");
✗ Incorrect
Option A correctly declares a fixed-size array of 3 string slices (&str). Option B misses the type but works due to type inference, so both A and B are correct. Option C is incorrect because String requires explicit construction. Option D is a tuple, not an array.
What is the index of the first element in a Rust array?
A0
B1
C-1
DDepends on the array
✗ Incorrect
Rust arrays are zero-indexed, so the first element is at index 0.
What does this code do? let arr = [5; 4];
ACreates an array with elements 1, 2, 3, 4, 5
BSyntax error
CCreates an array of 5 elements, all set to 4
DCreates an array of 4 elements, all set to 5
✗ Incorrect
The syntax [value; count] creates an array with 'count' elements, each set to 'value'. Here, 4 elements all set to 5.
What happens if you try to access arr[10] when arr has 5 elements?
AReturns the 10th element
BPanics at runtime
CReturns null
DCompiles but undefined behavior
✗ Incorrect
Rust checks array bounds at runtime and will panic if you access an invalid index.
How do you get the number of elements in an array arr?
Aarr.len()
Barr.length
Carr.size()
Dlen(arr)
✗ Incorrect
The method len() returns the length of an array in Rust.
Explain how to declare, initialize, and access elements in a Rust array.
Think about how you tell Rust the type and size, then how you get values.
You got /3 concepts.
What safety features does Rust provide when working with arrays?
Rust prevents common errors by checking indexes.
You got /3 concepts.
Practice
(1/5)
1. What is true about arrays in Rust?
easy
A. Their size can change after creation.
B. They store multiple values of the same type in a fixed-size list.
C. They can store values of different types together.
D. They do not allow access by index.
Solution
Step 1: Understand array type restrictions
Arrays in Rust hold values all of the same type, ensuring type safety.
Step 2: Understand array size behavior
Arrays have a fixed size that cannot be changed after creation.
Final Answer:
They store multiple values of the same type in a fixed-size list. -> Option B
Quick Check:
Array = fixed size + same type [OK]
Hint: Remember: Rust arrays are fixed size and single type [OK]
Common Mistakes:
Thinking arrays can hold mixed types
Assuming arrays can grow or shrink
Believing arrays don't use indexes
2. Which of the following is the correct way to declare an array of 3 integers in Rust?
easy
A. let arr = [1, 2, 3];
B. let arr = (1, 2, 3);
C. let arr = {1, 2, 3};
D. let arr = <1, 2, 3>;
Solution
Step 1: Identify Rust array syntax
Arrays use square brackets [] with comma-separated values.
Step 2: Check other options
Parentheses () create tuples, curly braces {} are blocks, angle brackets <> are not valid here.
Final Answer:
let arr = [1, 2, 3]; -> Option A
Quick Check:
Array syntax uses [] [OK]
Hint: Arrays use square brackets [] with commas [OK]
Common Mistakes:
Using parentheses instead of brackets
Confusing arrays with tuples
Using invalid symbols like {} or <>
3. What will be the output of this Rust code?
let arr = [10, 20, 30, 40];
println!("{}", arr[2]);
medium
A. Index out of bounds error
B. 20
C. 40
D. 30
Solution
Step 1: Understand array indexing
Rust arrays start indexing at 0, so arr[2] is the third element.
Step 2: Identify the element at index 2
The elements are [10, 20, 30, 40], so arr[2] is 30.
Final Answer:
30 -> Option D
Quick Check:
Index 2 = 30 [OK]
Hint: Count from zero to find the correct index [OK]
Common Mistakes:
Starting count at 1 instead of 0
Confusing element positions
Expecting runtime error for valid index
4. Find the error in this Rust code snippet:
let arr = [1, 2, 3];
println!("{}", arr[3]);
medium
A. No error, prints 3
B. Type mismatch error
C. Index out of bounds error at runtime
D. Syntax error due to missing semicolon
Solution
Step 1: Check array length and index used
The array has 3 elements with indexes 0, 1, 2. Index 3 is invalid.
Step 2: Understand Rust runtime behavior
Accessing arr[3] causes a runtime panic due to index out of bounds.
Final Answer:
Index out of bounds error at runtime -> Option C
Quick Check:
Invalid index causes runtime panic [OK]
Hint: Check if index is less than array length [OK]
Common Mistakes:
Assuming index 3 is valid for 3-element array
Confusing syntax error with runtime error
Ignoring Rust's panic on invalid index
5. You want to create an array of 5 zeros in Rust. Which code correctly does this?
hard
A. let arr = [0; 5];
B. let arr = [0, 5];
C. let arr = [5; 0];
D. let arr = [0] * 5;
Solution
Step 1: Understand Rust array repetition syntax
The syntax [value; count] creates an array repeating value count times.
Step 2: Check each option
let arr = [0; 5]; uses [0; 5], which creates 5 zeros. Others are invalid or create wrong arrays.
Final Answer:
let arr = [0; 5]; -> Option A
Quick Check:
Use [value; count] to repeat values [OK]
Hint: Use [value; count] to make repeated arrays [OK]
Common Mistakes:
Using commas instead of semicolon for repetition
Confusing multiplication syntax with arrays
Reversing value and count in repetition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.