Iterating over custom chunks

What I need is something like slice::windows() but custom-defined rather than just by size. E.g I'd like have that working:

let data = vec![1,1,3,3,3,2];
for chunk in data.split_by(|current,next| current==next) {
    // first iter: [1,1]
    // second iter: [3,3,3]
    // third iter: [2]

Although I may imagine numerous criteria for such splitting, at that moment my actual problem is to split a vector of object into slices where all objects in a slice are (sort of) equal.

For instance, imagine a vector of CustomerAddress structs; I need slices of objects where zip-code is identical.

This is what the unstable group_by does (currently blocking on naming issues). For now you can use the slice-group-by crate instead


Incidentally, writing your own iterator may not be as daunting as you think. You might want to take a look at the GroupBy implementation, which is a straight-forward slice iterator.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.