I have such a piece of code:
struct Node {
value: u32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
fn traverse(last_nodes: &mut VecDeque<&Node>, result: &mut Vec<u32>) {
if last_nodes.len() == 0 {
return;
}
let mut childrens = VecDeque::new();
while let Some(node) = last_nodes.pop_front() {
result.push(node.value);
if let Some(n) = &node.left {
childrens.push_back(&*n);
}
if let Some(n) = &node.right {
childrens.push_back(&*n);
}
}
traverse(&mut childrens, result)
}
pay more attention to below code, I want childrens to be a VecDeque<&Node> type
let mut childrens = VecDeque::new();
however, it gets a result like VecDeque<&Box<Node>> because of below code and inference of compiler
childrens.push_back(&*n);
but if we correct the above code to be more definate, we can get our codes passing the compiler
let mut childrens:VecDeque<&Node> = VecDeque::new();
I don't know if it is the imperfection of compiler