The array arr is declared without mut, so it is immutable and cannot be changed.
Step 2: Identify assignment error
The line arr[1] = 5; tries to modify an immutable array element, causing a compile-time error.
Final Answer:
Cannot assign to element of immutable array -> Option A
Quick Check:
Immutable arrays cannot be updated [OK]
Hint: Declare array with 'mut' to allow updates [OK]
Common Mistakes:
Ignoring mut keyword for mutability
Assuming index out of bounds error
Thinking syntax error causes failure
5. Given a mutable array arr of length 4 initialized as [2, 4, 6, 8], which code snippet correctly doubles the value at index 3 and then prints the updated array?
hard
A. arr[4] = arr[4] * 2;
println!("{:?}", arr);
B. arr[3] = arr[3] * 2;
println!("{:?}", arr);
C. arr[3] *= 2
println!("{:?}", arr);
D. arr[3] = arr[3] + 2;
println!("{:?}", arr);
Solution
Step 1: Identify correct index and operation
Index 3 is the fourth element (value 8). Doubling means multiplying by 2, so arr[3] = arr[3] * 2; is correct.
Step 2: Check syntax and print statement
arr[3] = arr[3] * 2;println!("{:?}", arr); uses correct syntax with semicolon and prints the updated array properly.
Final Answer:
arr[3] = arr[3] * 2;
println!("{:?}", arr); -> Option B
Quick Check:
Multiply element at index 3 by 2, then print [OK]
Hint: Use correct index and multiply, remember semicolons [OK]
Common Mistakes:
Using index 4 which is out of bounds
Forgetting semicolon after assignment
Adding 2 instead of multiplying by 2
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.