Reputation: 345
Here is my code:
struct Node<T> {
data: T,
next: Option<Box<Node<T>>>,
}
impl<T> Node<T> {
fn new(data : T) -> Node<T> {
Node { data: data, next: None }
}
fn new_with_next(data: T, next: Option<Box<Node<T>>>) -> Node<T> {
Node { data: data, next: next }
}
}
struct LinkedList<T> {
head: Box<Node<T>>,
size: u8,
}
impl<T> LinkedList<T> {
fn new(data: T) -> LinkedList<T> {
let new_node = Node::new(data);
let head = Box::new(new_node);
LinkedList { head: head, size: 1 }
}
fn insert(&mut self, data: T) {
let mut next = Some(self.head); // <-- error here
let new_node = Node::new_with_next(data, next);
self.head = Box::new(new_node);
self.size += 1;
}
}
I get this error:
src\linked_list.rs:28:29: 28:33 error: cannot move out of borrowed content [E0507]
src\linked_list.rs:28 let next = Some(self.head);
^~~~
I don't understand the error, nor how to fix it. I tried giving a reference to self.head
to Some
, however I'm changing the data type this inside Some
this way.
Upvotes: 3
Views: 3856
Reputation: 30101
The problem is that you take self
by reference, therefore you cannot move out its head
(it would become invalid), which let mut next = Some(self.head);
would do.
std::mem::replace
is a nice function for this:
fn insert(&mut self, data: T) {
let mut next = std::mem::replace(&mut self.head, Box::new(Node::new(data)));
self.head.next = Some(next);
self.size += 1;
}
It allows you to replace the head with a new node and get the old one at the same time, which Rust is happy about because head
stays valid all along now.
Upvotes: 9