I have a struct with an optional self-reference:
struct Pathnode {
pos: Pos,
parent: Option<Rc<Pathnode>>,
}
I want to follow the parent
reference until the first child before the root node (The root node has no parent). I tried the following code:
let mut head: Pathnode = node;
while head.parent.is_some() {
head = *head.parent.unwrap();
}
But this fails to compile with the following error:
error[E0507]: cannot move out of borrowed content
--> file.rs on line 134:24
|
139 | head = *head.parent.unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ cannot move out of borrowed content
How can I get a Pathnode
from the Rc
? Alternatively, what other data type could I use for head
? It's OK if I only get an immutable reference or similar at the end.