I'm learning Rust by implementing a single linked list. I've followed the 2nd chapter in Learning Rust With Entirely Too Many Linked Lists. I want to implement a function to set value of a specific element in the list by its index. Here's my code so far:
pub struct List {
head: Link,
}
enum Link {
Empty,
More(Box<Node>),
}
struct Node {
elem: i32,
next: Link,
}
impl List {
/// Replaces the element at the specified position in this list with the specified element.
pub fn set(&mut self, index: usize, elem: i32) -> Result<(), &str> {
// Start from the first node where index is 0
let mut id: usize = 0;
let mut curr_node: &Link = &self.head;
while let Link::More(mut node) = curr_node {
curr_node = &node.next;
id += 1;
if id == index {
node.elem = elem;
break;
}
}
if id < index {
return Err("Invalid index");
}
Ok(())
}
}
The compiler emits 2 compile errors:
error[E0507]: cannot move out of `curr_node.0` which is behind a shared reference
--> src/lib.rs:21:42
|
21 | while let Link::More(mut node) = curr_node {
| -------- ^^^^^^^^^
| |
| data moved here
| move occurs because `node` has type `Box<Node>`, which does not implement the `Copy` trait
error[E0597]: `node.next` does not live long enough
--> src/lib.rs:22:25
|
21 | while let Link::More(mut node) = curr_node {
| --------- borrow later used here
22 | curr_node = &node.next;
| ^^^^^^^^^^ borrowed value does not live long enough
...
28 | }
| - `node.next` dropped here while still borrowed
How should I implement this correctly?