# Impl DoubleEndedIterator for ExtractIf (née DrainFilter)?

**URL:** <https://internals.rust-lang.org/t/impl-doubleendediterator-for-extractif-nee-drainfilter/21039>\
**Category:** libs\
**Created:** [June 16, 2024, 8:39pm UTC](https://internals.rust-lang.org/t/impl-doubleendediterator-for-extractif-nee-drainfilter/21039 "2024-06-16T20:39:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [June 16, 2024, 8:39pm UTC](https://internals.rust-lang.org/t/impl-doubleendediterator-for-extractif-nee-drainfilter/21039/1 "2024-06-16T20:39:10Z")

</div>

Inspired by [this Reddit comment](https://old.reddit.com/r/rust/comments/1dgvcv9/what_methods_do_you_think_are_missing_or_would/l8tt3nv/), although it turned out that `pop_if` already exist on nightly.

[`Vec::extract_if`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.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`.)

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [June 16, 2024, 8:48pm UTC](https://internals.rust-lang.org/t/impl-doubleendediterator-for-extractif-nee-drainfilter/21039/2 "2024-06-16T20:48:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 14, 2024, 8:49pm UTC](https://internals.rust-lang.org/t/impl-doubleendediterator-for-extractif-nee-drainfilter/21039/3 "2024-09-14T20:49:03Z")

</div>

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