std-method
Vec::as_slice
Borrow all initialized elements as a shared slice, without cloning. A function accepting a slice can work with vectors and arrays alike. Expected stdout (not executed): [7, 9] Common mistake: Expecting the slice to allow pushing new elements.
Formal Syntax & Structure
rust syntaxrust
pub fn as_slice(&self) -> &[T]Working Example
Example Snippetrust
fn main() { let scores = vec![4, 7, 9]; let view = scores.as_slice(); println!("{:?}", &view[1..]);}