I implemented a simple Binary Search Tree in Rust (following CIS 198, it\'s great), and for learning I\'m doing iterators that just run through the right edges.
I c
I think you cannot split self
into 2 mutable objects (one for the Item
, one for self
itself) without using some unsafe code.
You need to change the type of the field IterMut::next
to Option<&'a mut Node<T>>
:
pub struct IterMut<'a, T: 'a> {
next: Option<&'a mut Node<T>>,
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
self.next.take().map(|node| {
self.next = node.right.0.as_mut().map(|node| &mut **node);
&mut node.elem
})
}
}
You can find more useful information about the implementation of the mutable iterator for recursive data structures in the IterMut chapter of Learning Rust With Entirely Too Many Linked Lists.