So, I'm learning Rust and decided to build a sorted linked list. Everything looks nice until I reach the add method, here's the code:
struct NodeItem<'a, V:'a + Ord> {
value : V,
next : Box<Option<NodeItem<'a,V>>> // '
}
impl <'a, V:'a + Ord> NodeItem<'a,V> { // '
fn new(value : V) -> NodeItem<'a,V> { // '
NodeItem { value : value, next : box None }
}
fn add(&mut self, value : V) {
match self.value.cmp(&value) {
Less => {
self.next = box Some(NodeItem {value: self.value, next : self.next });
self.value = value;
},
Equal | Greater => {
match *self.next {
Some(ref mut next) => next.add(value),
None => self.next = box Some(NodeItem::new(value)),
}
},
}
}
}
The compiler complains with:
/home/mauricio/projects/rust/data_structures/src/lists/mod.rs:16:47: 16:51 error: cannot move out of dereference of `&mut`-pointer
/home/mauricio/projects/rust/data_structures/src/lists/mod.rs:16 self.next = box Some(NodeItem {value: self.value, next : self.next });
^~~~
/home/mauricio/projects/rust/data_structures/src/lists/mod.rs:16:66: 16:70 error: cannot move out of dereference of `&mut`-pointer
/home/mauricio/projects/rust/data_structures/src/lists/mod.rs:16 self.next = box Some(NodeItem {value: self.value, next : self.next });
What is exactly the problem here? I understand that I am moving a reference somewhere else, but shouldn't the lifetime parameters show that these items have a related "life"?
This is using the nightly from 21/12/14.