Slice Types and Range Syntax

Updated

October 31, 2026

Slice Types and Range Syntax

Slices generalize beyond strings to any contiguous sequence, such as arrays and vectors.

fn main() {
    let numbers = [10, 20, 30, 40, 50];

    let slice: &[i32] = &numbers[1..4]; // Elements 20, 30, 40
    println!("Slice: {:?}", slice);

    // Range variations:
    let start_to_two = &numbers[..2];
    let three_to_end = &numbers[3..];
    let all = &numbers[..];

    println!("Start: {:?}, End: {:?}, All: {:?}", start_to_two, three_to_end, all);
}