Click a card to flip it · 8 terms
A slice is a borrowed view into part of a collection. This deck covers array slices, string slices, and how slices let you work with data without copying it.
A fixed-size collection where every element is the same type. Length is part of the type.
Storing a fixed number of values of identical type.
let scores: [i32; 5] = [10, 20, 30, 40, 50];
A borrowed view into part of a collection. Does not take ownership.
Working with part of an array or string without copying data.
let part = &scores[1..4];// elements at index 1, 2, 3
The type of an array: T is the element type, N is the fixed length.
Declaring array types explicitly in function signatures.
let addr: [u8; 32] = [0; 32];// 32 bytes, all zeroes
Returns the number of elements in a slice or array.
When you need the size to loop, check bounds, or compute positions.
let n = scores.len(); // 5for i in 0..scores.len() { }
A borrowed view into part of a String. Returns &str. The end index is excluded.
Getting a substring without owning or copying it.
let hello = &s[0..5];// end is excluded
Returns Option<&T> for an index. Safe access that returns None instead of panicking.
When the index might be out of bounds.
arr.get(10) // → Nonearr.get(0) // → Some(&val)
A slice reference holds two things: a pointer to the start, and the length.
Why slices must always be references &[T]. The compiler needs to know the size.
std::mem::size_of_val(&slice)// always 16 bytes (ptr + len)
Slicing strings uses byte offsets, not character positions. The end is excluded.
You cannot index Rust strings by character. Always slice by bytes.
&s[0..5] // first 5 bytes// panics if mid-character