How to reconcile IntoIter with impl Iterator?

⚓ Rust    📅 2026-01-16    👤 surdeus    👁️ 1      

surdeus

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?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed