Impl DoubleEndedIterator for ExtractIf (née DrainFilter)?

Inspired by this Reddit comment, although it turned out that pop_if already exist on nightly.

Vec::extract_if, combined with take or take_while can be used as generalized pop_if, ie. a conditional removal of zero or more elements from one of the ends of the vector – except that extract_if extracts from the front and pop_if from the back. The latter is, of course, also more efficient. The ExtractIf iterator does not impl DoubleEndedIterator although to me it seems it could. Does anyone see any problems with adding such an impl?

(Also, VecDeque would also benefit from having an extract_if, although to match the rest of the API there should probably be separate extract_front_if and extract_back_if.)

1 Like

Iterator adapters like Filter and Map are double-ended, so it would be consistent with that. On the other hand, retain and retain_mut explicitly visit elements in order. Since it does change the logical implications for the closure, a conscious decision (be it for or against) should be made pre-stabilization IMO.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.