Slices
Slicing an Array
A slice references a contiguous portion of a collection using range syntax inside square brackets, without copying the underlying elements.
Example: Slicing an Array
fn main() {
let numbers = [10, 20, 30, 40, 50];
let middle = &numbers[1..4];
println!("{:?}", middle);
}
Login to try C/C++/Java/PHP code in the editor
Slicing a Vector
Slices work the same way on a Vec, letting you view a portion of it as &[T] while the Vec itself keeps ownership of the data.
Example: Slicing a Vector
fn main() {
let scores = vec![90, 85, 77, 92];
let top_two = &scores[0..2];
println!("Top two: {:?}", top_two);
}
Login to try C/C++/Java/PHP code in the editor
Full Slices
Omitting both ends of a range, &collection[..], produces a slice covering the entire collection, which is useful when a function parameter expects a slice type rather than an owned collection.
Example: Full Slices
fn total(values: &[i32]) -> i32 {
values.iter().sum()
}
fn main() {
let data = vec![1, 2, 3, 4];
println!("Sum: {}", total(&data[..]));
}
Login to try C/C++/Java/PHP code in the editor
Slices Borrow Their Source
Because a slice is a reference, it follows the same borrowing rules: the original collection cannot be mutated while an active slice of it still exists.
Example: Slices Borrow Their Source
fn main() {
let letters = ['a', 'b', 'c', 'd'];
let part = &letters[1..3];
println!("Slice: {:?}", part);
println!("Original still usable: {:?}", letters);
}
Login to try C/C++/Java/PHP code in the editor
- Trying to slice out of bounds, e.g.
&array[0..10]on a 5-element array, which panics at runtime. - Forgetting slices borrow from the original data, so the original collection cannot be mutated while a slice of it is in use.
- Confusing
&vec[..](a slice of the whole vector) withvecitself (an owned value) when passing data to functions.
- A slice, written
&[T]or&str, references a contiguous sequence of elements without owning them. - Range syntax like
1..3selects a sub-range of a collection to slice. - Slices borrow from their source, so borrowing rules still apply to them.
- Slices let functions accept both full collections and partial views with one flexible parameter type.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: