Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johnfercher opened this issue Sep 1, 2018 · 1 comment
Open
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@johnfercher
Copy link

johnfercher commented Sep 1, 2018

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 }]]
@csmoe csmoe added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Sep 2, 2018
@oskgo
Copy link
Contributor

oskgo commented Jan 25, 2024

#67330 added the first case as split_inclusive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants