I have a structure with a parent property I want to add to a queue. The parent is the same type as itself, so I need to wrap it in a Box
.
use std::collections::vec_deque::VecDeque;
struct GraphNode {
value: u32,
parent: Option<Box<&GraphNode>>,
}
fn main() {
let mut queue: VecDeque<GraphNode> = VecDeque::new();
let parent = GraphNode {
value: 23,
parent: Option::None,
};
let second = GraphNode { value: 42, parent };
let third = GraphNode {
value: 19,
parent: Option::Some(Box::from(&parent)),
};
queue.push_front(parent);
queue.push_front(second);
queue.push_front(third);
}
error[E0106]: missing lifetime specifier
--> src/main.rs:5:24
|
5 | parent: Option<Box<&GraphNode>>,
| ^ expected lifetime paramete
The parent can be null, so I get that it needs to be Box<Option<&GraphNode>>
, but I get the error expected lifetime parameter
, however what's in the docs isn't really making sense to me.
There's also the issue that when I create a Box
, to save to the parent, the value is moved. I don't want to move the value, I just want to save a reference in the box.