How to reconcile IntoIter with impl Iterator?

Let's have a struct which implements function returning consuming iterator:

impl<T: PartialEq + Debug + Default> List<T> {
    pub fn iter_mut(&mut self) -> impl Iterator<Item = T> {
        let mut curr = self.0.take();
        std::iter::from_fn(move || {
            match curr.take() {
                None => None,
                Some(node) => {
                    curr = node.next.0;
                    Some(node.value)
                }
            }
        })
    }
}

Now I am trying to implement IntoIterator in terms of this function. Obvious take however fails with

error[E0658]: impl Trait in associated types is unstable

impl<T: PartialEq + Debug + Default> IntoIterator for List<T> {
    type Item = T;
    type IntoIter = impl Iterator<Item = Self::Item>;

    fn into_iter(mut self) -> Self::IntoIter {
        self.iter_mut()
    }
}

What should I do in stable Rust, when impl Trait is unstable in associated types? Any idea how to reconcile both, IntoIterator's IntoIter with FromFn in current state of affairs?

the correct thing is to bite the bullet and actually write an iterator by hand.

also i would strongly advise against calling it iter_mut, as it simply removes all items out of the source.

call it drain or smth.

and if you do want to have such a method with an impl Iterator, you should prob use + use <> here

3 Likes

the correct thing is to bite the bullet and actually write an iterator by hand

I was going to do this as the last resort. It would be however quite unfortunate if there is no other way then duplicate code instead of reuse.

you don't need to duplicate anything, once you've made your real iterator you can just reuse it