Rust Slices Quiz
40 comprehensive questions on Rust's slice types, covering borrowed views, indexing, mutable slices, ranges, and slice methods — with 11 code examples demonstrating practical slice usage patterns.
Question 1
What is a slice in Rust and how does it differ from an array or vector in terms of memory ownership and allocation?
Question 2
How do you create a slice from an array and what is the syntax for specifying the range of elements you want to include?
Question 3
What happens when you try to access a slice element using an index that is out of bounds, and how does this compare to array access?
Question 4
How do you safely access slice elements to avoid potential panics, and what type does the safe access method return?
Question 5
What will this code output when executed, and why does it work the way it does?
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
println!("Slice length: {}, first element: {}", slice.len(), slice[0]);
}Question 6
How do you create a mutable slice from a mutable array, and what operations become possible with a mutable slice that aren't possible with an immutable slice?
Question 7
What is the difference between slice.len() and slice.capacity() for slices created from arrays versus slices created from vectors?
Question 8
How do you iterate over all elements of a slice using a for loop, and what are the different ways to get mutable access during iteration?
Question 9
What does the range syntax .. represent when creating slices, and how does it differ from ..= syntax?
Question 10
How do you create a slice that includes all elements of an array or vector, and what is this commonly called?
Question 11
What will this code do and why might it be useful in practice?
fn main() {
let data = [10, 20, 30, 40, 50];
let middle = &data[1..4]; // elements 20, 30, 40
println!("Middle slice: {:?}", middle);
// Can still access original array
println!("First element: {}", data[0]);
}Question 12
How do slice methods like first() and last() work, and what do they return when called on an empty slice?
Question 13
What does the split_at() method do on a slice, and when might you use it instead of creating two separate slices?
Question 14
How do you check if a slice contains a specific element, and what method would you use for more complex search conditions?
Question 15
What happens when you try to modify a slice element through a mutable slice reference, and how does this affect the original data source?
Question 16
How do you create a slice from a vector, and what is the relationship between the slice's lifetime and the vector's lifetime?
Question 17
What does the chunks() method do on a slice, and how might it be useful for processing large datasets in fixed-size pieces?
Question 18
How do you reverse the elements of a slice in place, and what type of slice reference do you need for this operation?
Question 19
What is the difference between slice concatenation using iterators versus using the concat() method, and when would you choose one over the other?
Question 20
How do you handle the case where you need to work with a slice but also need to know the total length of the original data structure it came from?
Question 21
What will this code output and why does the borrowing work the way it does?
fn main() {
let mut data = [1, 2, 3, 4, 5];
let slice1 = &mut data[0..3]; // mutable borrow of first 3 elements
let slice2 = &data[2..5]; // immutable borrow of last 3 elements
slice1[0] = 10;
println!("slice2[0] = {}", slice2[0]); // This works!
}Question 22
How do you create a slice from a string literal, and what type does this create in Rust?
Question 23
What does the windows() method do on a slice, and how does it differ from chunks() in terms of overlap between consecutive elements?
Question 24
How do you sort a mutable slice in place, and what trait bounds are required on the element type?
Question 25
What happens when you try to create a slice with invalid range bounds, such as negative indices or end before start?
Question 26
How do you convert a slice to a vector, and when might you need to do this conversion?
Question 27
What does the binary_search() method do on a sorted slice, and what does it return for successful and unsuccessful searches?
Question 28
How do you create a slice that skips elements at the beginning or end of another slice, and what methods would you use for this?
Question 29
What is the relationship between slices and the concept of 'fat pointers' in Rust's memory model?
Question 30
How do you handle slices of different types in generic functions, and what trait bounds might you need?
Question 31
What will this code demonstrate about slice borrowing rules?
fn main() {
let mut data = [1, 2, 3, 4, 5];
let slice = &mut data[..]; // mutable borrow of entire array
// This would fail:
// let another = &data[0]; // immutable borrow while mutable borrow exists
slice[0] = 10;
println!("Modified: {}", slice[0]);
}Question 32
How do you implement a function that accepts any slice type, including both arrays and vectors, using generic programming?
Question 33
What does the rotate_left() and rotate_right() methods do on mutable slices, and when might you use these operations?
Question 34
How do you compare two slices for equality, and what does the comparison check exactly?
Question 35
What is the difference between slice::from_ref() and creating a slice directly with &value, and when would you use each approach?
Question 36
How do you handle slices in error-prone code where bounds checking might fail, and what patterns help make slice operations more robust?
Question 37
What does the copy_from_slice() method do, and what are the requirements for it to work correctly?
Question 38
How do you work with slices of slices (nested slices), and what types are involved in this pattern?
Question 39
What performance characteristics do slice operations have compared to operations on owned collections like Vec?
Question 40
In a complex algorithm that needs to process different overlapping views of the same data, how would you manage slice lifetimes and borrowing to avoid compilation errors?
