Skip to content

Provide a split method which doesn't consume the element used to split #53890

Description

@johnfercher

The split method consumes the element used to split, how you can see in the code below. But in some cases would be nice to move the element to the "first or second slice" like the split_at method does, but to n slices.

The numpy library has a method like that.

https://play.rust-lang.org/?gist=94dd413054604e4efffa96a5e8ed72b5&version=stable&mode=debug&edition=2015

Code

#[derive(Clone, Debug)]
struct Delta {
    pub val: i32
}

fn main() {
    let mut v = vec![Delta{ val: 1 }, Delta{ val: 5 }, Delta{ val: 1 }];
    
    let new_v : Vec<Vec<Delta>> = v.split(|x| x.val > 4)
                    .into_iter()
                    .map(|x| x.to_vec())
                    .collect();
                    
    println!("{:?}", new_v);
}

Output

[[Delta { val: 1 }], [Delta { val: 1 }]]

New Split Method1 Output

[[Delta { val: 1 }, Delta { val: 5 }], [Delta { val: 1 }]]

New Split Method2 Output

[[Delta { val: 1 }], [Delta { val: 5 }, Delta { val: 1 }]]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions