I recently came across a situation where I wanted to get the indices of each occurrence of a value in a slice. Basically, something like str.match_indices
, but for a &[u8]
. I was surprised to learn that there wasn’t anything like that in the standard library. The best thing I could find would be to do something like
slice.iter().enumerate().filter(|(_,x)| x == needle).map(|(i,_)| i)
I expected there to be something like
slice.iter().positions(|x| x == needle)
Do you think a positions method should be added to Iterator in the standard library? Or to slice? If so, I’d be happy to make a PR and/or RFC.
- Include
Iterator.positions
to standard library - Add
slice.positions
to standard library - This doesn’t belong in the standard library
0 voters