std-method

Vec::split_off

Split at an index, leaving the prefix in the original and returning the owned suffix. The index may equal len, but may not exceed it. The original vector retains its capacity. Expected stdout (not executed): ["A"] ["B", "C"] Common mistake: Expecting the split position to remain in the prefix.

Formal Syntax & Structure

rust syntaxrust
pub fn split_off(&mut self, at: usize) -> Vec<T>

Working Example

Example Snippetrust
fn main() {    let mut jobs = vec!["A", "B", "C"];    let later = jobs.split_off(1);    println!("{jobs:?} {later:?}");}
Rust Reference