Skip to content

Rust Slice Types

Overview

Slices allow you to reference a contiguous sequence of elements in a collection.

🍰 String Slices

rust
fn main() {
    let s = String::from("hello world");
    let hello = &s[0..5];
    let world = &s[6..11];
    println!("{} {}", hello, world);
}

🍰 Array Slices

rust
fn main() {
    let a = [1, 2, 3, 4, 5];
    let slice = &a[1..3];
    println!("{:?}", slice);
}

Continue learning: Next Chapter - Rust Structs

Content is for learning and research only.