Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a slice from the array.
Rust
let arr = [1, 2, 3, 4, 5]; let slice = &arr[1]; println!("{:?}", slice);
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that is out of bounds.
Forgetting the & to borrow the slice.
✗ Incorrect
The slice &arr[..] creates a slice of the whole array.
2fill in blank
mediumComplete the code to get a slice of the first three elements.
Rust
let arr = [10, 20, 30, 40, 50]; let first_three = &arr[1]; println!("{:?}", first_three);
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range starting at 3 instead of ending at 3.
Using incorrect indices.
✗ Incorrect
The slice &arr[..3] gets elements from start up to index 3 (exclusive).
3fill in blank
hardFix the error in the slice syntax to borrow a slice of the last two elements.
Rust
let arr = [5, 6, 7, 8, 9]; let last_two = &arr[1]; println!("{:?}", last_two);
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that starts too late or too early.
Using a range that is out of bounds.
✗ Incorrect
The slice &arr[3..5] gets elements at indices 3 and 4 (last two elements).
4fill in blank
hardComplete the code to create a slice of elements from index 1 to 3.
Rust
let arr = 100, 200, 300, 400, 500]; let mid_slice = &arr[[1]; println!("{:?}", mid_slice);
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting brackets.
Using incorrect range syntax.
✗ Incorrect
.3] to get elements at indices 1 and 2.
5fill in blank
hardFill both blanks to create a slice of the middle three elements.
Rust
let arr = 7, 14, 21, 28, 35, 42]; let middle = &arr[[1][2]; println!("{:?}", middle);
Attempts:
3 left
💡 Hint
Common Mistakes
Missing brackets.
Using wrong range endpoints.
✗ Incorrect
.4] gets elements at indices 1, 2, and 3.
