Content-Length: 16874 | pFad | https://doc.rust-lang.org/reference/items/../names/../types/../items/../types/slice.html

Slice types - The Rust Reference

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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].

Slice types are generally used through pointer types. For example:

  • &[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’. It mutably borrows the data it points to.
  • Box<[T]>: a ‘boxed slice’

Examples:

#![allow(unused)]
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.









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/reference/items/../names/../types/../items/../types/slice.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy