RFC: Add the group_by and group_by_mut methods to slice by Kerollmops · Pull Request #2477 · rust-lang/rfcs (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

Kerollmops

@Centril Centril added the T-libs-api

Relevant to the library API team, which will review and decide on the RFC.

label

Jun 15, 2018

@Kerollmops

bors added a commit to rust-lang-ci/rust that referenced this pull request

Dec 31, 2020

@bors

The return of the GroupBy and GroupByMut iterators on slice

According to rust-lang/rfcs#2477 (comment), I am opening this PR again, this time I implemented it in safe Rust only, it is therefore much easier to read and is completely safe.

This PR proposes to add two new methods to the slice, the group_by and group_by_mut. These two methods provide a way to iterate over non-overlapping sub-slices of a base slice that are separated by the predicate given by the user (e.g. Partial::eq, |a, b| a.abs() < b.abs()).

let slice = &[1, 1, 1, 3, 3, 2, 2, 2];

let mut iter = slice.group_by(|a, b| a == b);
assert_eq!(iter.next(), Some(&[1, 1, 1][..]));
assert_eq!(iter.next(), Some(&[3, 3][..]));
assert_eq!(iter.next(), Some(&[2, 2, 2][..]));
assert_eq!(iter.next(), None);

An RFC was open 2 years ago but wasn't necessary.