Complete the code to access the element at index 2 in the array.
let arr = [10, 20, 30, 40, 50]; let value = arr[[1]]; println!("{}", value);
The index 2 accesses the third element in the array, which is 30.
Complete the code to update the element at index 1 to 100.
let mut arr = [10, 20, 30, 40]; arr[[1]] = 100; println!("{:?}", arr);
Index 1 is the second element, which we update to 100.
Fix the error in the code to correctly update the last element of the array.
let mut arr = [5, 10, 15, 20]; arr[[1]] = 25; println!("{:?}", arr);
The last element is at index 3 because indexes start at 0.
Fill both blanks to create a new array with each element doubled from the original array.
let arr = [1, 2, 3, 4]; let mut doubled = [[1]]; for i in 0..[2] { doubled[i] = arr[i] * 2; } println!("{:?}", doubled);
We create a new array of length 4 initialized with zeros using [0; 4]. The loop runs from 0 to arr.len() to double each element.
Fill all three blanks to create a function that updates the element at a given index with a new value.
fn update_element(arr: &mut [i32], [1]: usize, [2]: i32) { arr[[3]] = [2]; } fn main() { let mut numbers = [10, 20, 30]; update_element(&mut numbers, 1, 99); println!("{:?}", numbers); }
The function takes a mutable array, an index, and a value. It updates the element at the given index with the new value.
