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.
let v = [10, 20, 30]; assert_eq!(v.first(), Some(&10)); let (left, right) = v.split_at(1);
Try it
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.
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
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 ofv[0]. Use it when the slice may be empty; use indexing when emptiness would be a bug worth a panic.mid == lenis valid and gives an empty right part;mid == 0gives an empty left part.split_at_mutsolves "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_mutandget_disjoint_mut(Rust 1.86).- Values, not references:
first()givesOption<&T>. ForCopytypes writev.first().copied(), or.cloned()forClonetypes. - Slice patterns (
[first, .., last],[head, tail @ ..]) do the same job declaratively; see Slice patterns. - Splitting a
strusessplit_aton a byte offset, which panics inside a multi-byte character;split_at_checkedavoids that. - Related:
split_offon aVeccreates an owned tail, seeextend, append, split_off;iter().next()is another way to get the first element.