Tsallaka zuwa abun ciki
Justice Tech School

Slices

A slice borrows a part of a collection: &s[1..3] for strings and &v[..2] for vectors. Slices are fat pointers holding a start and a length.

Example
let v = vec![1, 2, 3, 4, 5];
let part = &v[1..4];
println!("{:?} {}", part, part.len());
let s = String::from("hello world");
let first = s.split(' ').next().unwrap();
println!("{}", first);

Result

[2, 3, 4] 3
hello

Exercise

Slice from index 1 to 3.

&v[14]

Quiz Test

Does a slice own its data?

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