std-method

slice::rsplitn

Limit reverse splitting to at most n groups. The final group can contain the unsplit prefix, useful when only the last field matters. Expected stdout (not executed): [[7], [5, 0, 6]] Common mistake: Expecting the unsplit prefix to be returned first.

Formal Syntax & Structure

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

Working Example

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