Skip to content
Justice Tech School

Slices

A slice borrows part of a collection: &v[1..3] or &s[0..2].

Example
fn main() {
    let v = vec![1, 2, 3, 4, 5];
    let s = &v[1..4];
    println!("{:?} {}", s, s.len());
    let t = "hello";
    println!("{}", &t[..2]);
}

Result

[2, 3, 4] 3
he

Exercise

Slice syntax.

&v[13]

Quiz Test

Does a slice own data?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.