Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: cannot move out of borrowed content for self field

Tags:

rust

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.

like image 749
Planet_Earth Avatar asked Jan 27 '26 07:01

Planet_Earth


1 Answers

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.

like image 164
mcarton Avatar answered Jan 28 '26 21:01

mcarton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!