Array slices let you look at part of an array without copying it. They help you work with sections of data easily and safely.
Array slices in Rust
fn main() {
let array = [10, 20, 30, 40, 50];
let slice: &[i32] = &array[1..4];
// slice includes elements from index 1 up to but not including 4
}Slices are references to a part of an array, so they do not own the data.
The syntax &array[start..end] creates a slice from index start up to but not including end.
fn main() {
let numbers = [1, 2, 3, 4, 5];
let full_slice = &numbers[..];
println!("Full slice: {:?}", full_slice);
}fn main() {
let numbers = [1, 2, 3, 4, 5];
let empty_slice = &numbers[2..2];
println!("Empty slice: {:?}", empty_slice);
}fn main() {
let numbers = [1, 2, 3, 4, 5];
let single_element_slice = &numbers[3..4];
println!("Single element slice: {:?}", single_element_slice);
}fn main() {
let numbers = [1, 2, 3, 4, 5];
let end_slice = &numbers[3..];
println!("Slice from index 3 to end: {:?}", end_slice);
}This program shows how to create slices from an array of fruit names. It prints the original array, a slice of part of the array, the full slice, and an empty slice.
fn main() {
let fruits = ["apple", "banana", "cherry", "date", "elderberry"];
println!("Original array: {:?}", fruits);
// Slice from index 1 to 4 (banana, cherry, date)
let fruit_slice = &fruits[1..4];
println!("Slice of fruits from index 1 to 3: {:?}", fruit_slice);
// Slice the whole array
let full_slice = &fruits[..];
println!("Full slice: {:?}", full_slice);
// Empty slice example
let empty_slice = &fruits[2..2];
println!("Empty slice: {:?}", empty_slice);
}Time complexity to create a slice is O(1) because it just creates a reference.
Space complexity is O(1) since no new data is copied.
Common mistake: Trying to slice with invalid indexes causes a panic at runtime.
Use slices when you want to borrow parts of arrays safely without copying.
Array slices let you borrow parts of arrays without copying.
Slices are references and do not own the data.
Use slices to work safely and efficiently with parts of arrays.
Practice
Solution
Step 1: Understand what a slice is
A slice is a reference to a contiguous part of an array, not a new array.Step 2: Compare options
Only A reference to a part of an array without copying data correctly describes a slice as a reference without copying.Final Answer:
A reference to a part of an array without copying data -> Option DQuick Check:
Slice = reference to part of array [OK]
- Thinking slices create new arrays
- Confusing slices with functions
- Assuming slices own data
arr in Rust?Solution
Step 1: Recall Rust slice syntax
Rust uses &array[start..end] to create a slice referencing elements from start up to but not including end.Step 2: Check each option
let slice = &arr[1..4]; uses correct Rust syntax. Options A, B, and C use invalid syntax.Final Answer:
let slice = &arr[1..4]; -> Option AQuick Check:
Slice syntax = &arr[start..end] [OK]
- Using parentheses instead of brackets
- Using colons instead of two dots
- Calling slice as a method
let arr = [10, 20, 30, 40, 50];
let slice = &arr[1..4];
println!("{:?}", slice);Solution
Step 1: Understand the slice range
The slice &arr[1..4] includes elements at indices 1, 2, and 3 (20, 30, 40).Step 2: Check the printed output
Printing the slice shows the elements in the slice as an array-like list.Final Answer:
[20, 30, 40] -> Option CQuick Check:
Slice arr[1..4] = [20, 30, 40] [OK]
- Including the element at index 4
- Starting slice at index 0
- Printing the slice as indices
let arr = [1, 2, 3, 4, 5];
let slice = &arr[2..6];
println!("{:?}", slice);Solution
Step 1: Check the slice range
The array has length 5, valid indices are 0 to 4. The slice uses 2..6, which tries to access index 5 (out of bounds).Step 2: Identify the error type
Rust will panic at runtime due to out-of-bounds slicing.Final Answer:
The slice range goes out of bounds -> Option AQuick Check:
Slice range must be within array length [OK]
- Using an end index beyond array length
- Confusing syntax errors with runtime errors
- Assuming arrays auto-expand
fn sum_slice(slice: &[i32]) -> i32 {
// fill in
}Solution
Step 1: Understand slice methods
A slice has an iterator via .iter(), and iterators have a .sum() method to add elements.Step 2: Evaluate each option
slice.iter().sum() correctly uses slice.iter().sum(). slice.to_vec().sum() is valid because Vec<i32> has sum() method. slice[0] + slice[1] only sums two elements, not all. slice.sum() is invalid because slices don't have sum() directly.Final Answer:
slice.iter().sum() -> Option BQuick Check:
Use iter().sum() to sum slice elements [OK]
- Trying to call sum() directly on slice
- Summing only first two elements
- Calling sum() on Vec<i32>
Ready to prove it?
Pass the test and this pattern moves unknown
