Slice [T]
is a region of an array or vector. Since u8
is just defined as a contiguous block of memory of type T
, There's no compile time definition as to how large it is. That's why Slice can't put it on the stack. As a same word, Slice can not be defined as a local variable. That's the reason why Slices
are always passed by reference.
The slice itself is the data(non sized). Many times though, people refer to &[T]
as a slice
A Reference to a slice is a fat pointer
.
Fat pointer
comprises two word value
1. First element
2. The number of elements
// Example code
let v: Vec<f64> = vec![0.0, 0.707, 1.0, 0.707];
let a: [f64; 4] = [0.0, -0.707, -1.0, -0.707];
let sv: &[f64] = &v;
let sa: &[f64] = &a;
In the last two lines, Rust automatically converts the &Vec reference and the &[f64; 4] reference to slice references that point directly to the data.
Slice
Reference to Slice