Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Collections

Rust slice first, last and split_at

Reference for Rust slice first, last, split_first, split_last, split_at, split_at_mut and get: returning Option, safe access and disjoint borrows.

These methods read the ends of a slice or cut it in two. first() and last() return Option<&T>, so an empty slice gives None instead of a panic. split_first() and split_last() separate one end element from the rest. split_at(mid) cuts a slice into a left and a right part at any position.

Because they return references into the original data, nothing is copied. The _mut versions return mutable references, and split_at_mut is the standard way to get two non-overlapping &mut views of one slice at the same time.

Rust
let v = [10, 20, 30];
assert_eq!(v.first(), Some(&10));
let (left, right) = v.split_at(1);

Try it

Rust Ends and splits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Mutating through the ends

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
impl<T> [T] {
    pub fn first(&self) -> Option<&T>;
    pub fn last(&self) -> Option<&T>;
    pub fn first_mut(&mut self) -> Option<&mut T>;
    pub fn last_mut(&mut self) -> Option<&mut T>;
    pub fn split_first(&self) -> Option<(&T, &[T])>;
    pub fn split_last(&self) -> Option<(&T, &[T])>;
    pub fn split_at(&self, mid: usize) -> (&[T], &[T]);
    pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T]);
    pub fn split_at_checked(&self, mid: usize) -> Option<(&[T], &[T])>;
}
Name Description
mid The split position. The left part has mid elements, the right part has len - mid.

Returns: Options for the end accessors (None when empty) and a pair of sub-slices for the splits. split_last returns the last element first, then the elements before it.

Panics: split_at and split_at_mut panic if mid > len. split_at_checked returns None instead.

Usage notes

  • first() is the safe form of v[0]. Use it when the slice may be empty; use indexing when emptiness would be a bug worth a panic.
  • mid == len is valid and gives an empty right part; mid == 0 gives an empty left part.
  • split_at_mut solves "two mutable borrows of one vector." Rust cannot prove &mut v[0] and &mut v[1] are disjoint, but it trusts the method that splits first. Other disjoint tools: iter_mut, chunks_mut, split_first_mut and get_disjoint_mut (Rust 1.86).
  • Values, not references: first() gives Option<&T>. For Copy types write v.first().copied(), or .cloned() for Clone types.
  • Slice patterns ([first, .., last], [head, tail @ ..]) do the same job declaratively; see Slice patterns.
  • Splitting a str uses split_at on a byte offset, which panics inside a multi-byte character; split_at_checked avoids that.
  • Related: split_off on a Vec creates an owned tail, see extend, append, split_off; iter().next() is another way to get the first element.