For now, this reference is a best-effort document. We strive for validity and completeness, but are not yet there. In the future, the docs and lang teams will work together to figure out how best to do this. Until then, this is a best-effort attempt. If you find something wrong or missing, file an issue or send in a pull request.

Slice types

Syntax
SliceType :
   [ Type ]

A slice is a dynamically sized type representing a 'view' into a sequence of elements of type T. The slice type is written as [T].

To use a slice type it generally has to be used behind a pointer for example as:

  • &[T], a 'shared slice', often just called a 'slice', it doesn't own the data it points to, it borrows it.
  • &mut [T], a 'mutable slice', mutably borrows the data it points to.
  • Box<[T]>, a 'boxed slice'

Examples:


# #![allow(unused_variables)]
#fn main() {
// A heap-allocated array, coerced to a slice
let boxed_array: Box<[i32]> = Box::new([1, 2, 3]);

// A (shared) slice into an array
let slice: &[i32] = &boxed_array[..];
#}

All elements of slices are always initialized, and access to a slice is always bounds-checked in safe methods and operators.