Slices
Borrowed views.
What Is a Slice?
A slice is a borrowed view into a contiguous part of a collection. It does not own data — it points into existing data.
Slices are written with a range inside brackets.
fn main() {
let arr = [1, 2, 3, 4, 5];
let part = &arr[1..4];
println!("{:?}", part);
}Slice Range Syntax
Ranges select a start and end index. The end is exclusive.
&v[1..4]— indices 1, 2, 3.&v[..3]— from start to 3.&v[2..]— from 2 to the end.
fn main() {
let v = vec![10, 20, 30, 40];
println!("{:?}", &v[..2]);
println!("{:?}", &v[2..]);
}