I find it odd that Iterator::take_while
takes ownership of the iterator. It seems like a useful feature to be able to take the first x elements which satisfy some function but still leave the rest of the elements available in the original iterator.
I understand that this is incompatible with a lazy implementation of take_while
, but still feels useful. Was this just judged not useful enough to include in the standard library, or is there some other problem I'm not seeing?
All the iterator adapters take the original iterator by value for efficiency's sake. Additionally, taking ownership of the original iterator avoids having to deal with lifetimes when it isn't necessary.
If you wish to retain access to the original iterator, you can use
by_ref
. This introduces one level of indirection, but the programmer chooses to opt into the extra work when the feature is needed:Has the output
Did you note that
4
was missing? That's because oncetake_while
picks a value and decides to not use it, there's nowhere for it to "put it back". Putting it back would require opting into more storage and slowness than is always needed.I've used the itertools crate to handle cases like this, specifically
take_while_ref
.