# Implementing an Iterator of mutable references

**URL:** <https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671>\
**Category:** help\
**Created:** [January 3, 2017, 1:32am UTC](https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671 "2017-01-03T01:32:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![radix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/radix/32/4539_2.png) [@radix](https://users.rust-lang.org/u/radix)\
**Post date:** [January 3, 2017, 1:32am UTC](https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671/1 "2017-01-03T01:32:34Z")

</div>

I have a custom data structure which is essentially a non-empty vector:

```
struct NE<T> {
    head: T,
    rest: Vec<T>
}

impl<T> NE<T> {
    pub fn get(&self, idx: usize) -> Option<&T> {
        if idx == 0 {
            Some(&self.head)
        } else {
            self.rest.get(idx - 1)
        }
    }

    pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
        if idx == 0 {
            Some(&mut self.head)
        } else {
            self.rest.get_mut(idx - 1)
        }
    }
}

```

I was able to create an Iterator for this type:

```
pub struct NEIter<'a, T: 'a> {
    ne: &'a NE<T>,
    current: usize,
}

impl<'a, T> Iterator for NEIter<'a, T> {
    type Item = &'a T;

    #[inline]
    fn next(&mut self) -> Option<&'a T> {
        let r = self.ne.get(self.current);
        self.current += 1;
        r
    }
}

```

The problem comes when I want to implement an iterable of _mutable_ references, similar to how Vec provides .iter\_mut(). That is, I want my iterator items to be `&'a mut T`.

I've tried a number of solutions, including by trying to implement my own custom NEIterMut with associated Iterator impl, as well as trying to implement it by chaining together a Some::iter\_mut with a Vec::iter\_mut, but I can't seem to get it to work. I have tried many different things and they all seemed like dead ends, so right now I'm just looking for general advice: is this even possible, given my current data structure?

I see unsafe blocks in the implementation of the Iterator for Vec in the stdlib; is it possible to implement a mutable iterator for this datatype using safe rust? I have not been able to find any applicable documentation for how to implement mutable iterators.

---

<div class="post-metadata">

**Author:** ![stebalien](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/stebalien/32/34_2.png) [@stebalien](https://users.rust-lang.org/u/stebalien)\
**Post date:** [January 3, 2017, 3:49am UTC](https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671/2 "2017-01-03T03:49:49Z")

</div>

Your chaining approach was correct. Unfortunately, implementing this directly is impossible without using `unsafe` because rust can't statically guarantee that you won't call `get_mut` twice with the same index (getting two mutable references to the same item).

```rust
use std::iter;
use std::slice::IterMut;

struct NE<T> {
    head: T,
    rest: Vec<T>
}

impl<T> NE<T> {
    pub fn get(&self, idx: usize) -> Option<&T> {
        if idx == 0 {
            Some(&self.head)
        } else {
            self.rest.get(idx - 1)
        }
    }

    pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
        if idx == 0 {
            Some(&mut self.head)
        } else {
            self.rest.get_mut(idx - 1)
        }
    }
    pub fn iter_mut(&mut self) -> NEIterMut<T> {
        NEIterMut {
            iter: iter::once(&mut self.head).chain(self.rest.iter_mut()),
        }
    }
}
pub struct NEIterMut<'a, T: 'a> {
    iter: iter::Chain<iter::Once<&'a mut T>, IterMut<'a, T>>,
}

impl<'a, T> Iterator for NEIterMut<'a, T> {
    type Item = &'a mut T;

    #[inline]
    fn next(&mut self) -> Option<&'a mut T> {
        self.iter.next()
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

```

Note: You don't _have_ to use chain, once, etc. You could also define `NEIterMut` as:

```rust
pub struct NEIterMut<'a, T: 'a> {
    head: Option<&'a mut T>,
    rest: IterMut<'a, T>,
}

```

and manually implement the iterator protocol.

---

<div class="post-metadata">

**Author:** ![radix](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/radix/32/4539_2.png) [@radix](https://users.rust-lang.org/u/radix)\
**Post date:** [January 3, 2017, 6:10am UTC](https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671/3 "2017-01-03T06:10:21Z")

</div>

Thank you very much, @stebalien. I didn't know about `iter::once`, I was trying Some::iter / iter\_mut and was struggling with it... though it might work too. I need to spend some time figuring out what the differences are between my attempts and your successful version. I'll also probably implement the manual version to see what I need to do.

I guess your strategy here is to take advantage of Vec's Iterator so we don't need to implement the unsafe stuff ourselves. My own non-chaining attempts were using my `get_mut` directly instead of using Vec's iterator, so that was probably my main problem.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:42am UTC](https://users.rust-lang.org/t/implementing-an-iterator-of-mutable-references/8671/4 "2023-01-12T08:42:19Z")

</div>

Closed on January 12, 2023, 8:42am UTC


