std-method

slice::splitn

Limit the number of returned groups, leaving the unprocessed suffix together in the last group. With n equal to zero, no groups are produced. Expected stdout (not executed): [[5], [6, 0, 7]] Common mistake: Treating n as the number of separators removed rather than the maximum group count.

Formal Syntax & Structure

rust syntaxrust
slice.splitn(n, |item| predicate)

Working Example

Example Snippetrust
fn main() {    let data = [5, 0, 6, 0, 7];    let groups: Vec<_> = data.splitn(2, |n| *n == 0).collect();    println!("{groups:?}");}
Rust Reference