primitive-type

Rust slice: view part of a sequence without copying it

A slice describes a contiguous sequence whose length is not part of its type. A borrowed slice, written &[T], is a view into existing elements and does not copy them. Mutable slices allow exclusive access to their elements. Expected output (not executed): [5, 7] Why: The half-open range starts at index 1 and stops before index 3. Common mistake: A slice type [T] is dynamically sized; use a reference or another suitable pointer in ordinary local bindings.

Formal Syntax & Structure

rust syntaxrust
[T]; &[T]; &mut [T]

Working Example

Example Snippetrust
fn main() {    let prices = [3, 5, 7, 9];    let middle: &[i32] = &prices[1..3];    println!("{middle:?}");}
Rust Reference