Complete the code to create a slice of the first three elements.
let arr = [1, 2, 3, 4, 5]; let slice = &arr[1]; println!("{:?}", slice);
The syntax [..3] creates a slice from the start up to, but not including, index 3.
Complete the code to create a slice from index 2 to the end.
let arr = [10, 20, 30, 40, 50]; let slice = &arr[1]; println!("{:?}", slice);
The syntax [2..] creates a slice starting from index 2 to the end.
Fix the error in the slice syntax to get elements from index 1 to 3.
let arr = [5, 6, 7, 8, 9]; let slice = &arr[1]; println!("{:?}", slice);
Rust slice syntax uses start..end inside square brackets, so [1..3] is correct.
Complete the code to create a slice from index 1 to 4 (exclusive).
let arr = [2, 4, 6, 8, 10]; let slice = &arr[[1]]; println!("{:?}", slice);
The slice syntax requires brackets around the range: [1.4].
Complete the code to create a slice from the start to index 3 (exclusive) and print its length.
let arr = [7, 14, 21, 28, 35]; let slice = &arr[[1]]; println!("Length: {}", slice.len());
.. without specifying the end index.The slice syntax is [.3] with brackets around the range.
