我有下面的rust结构,它有一个到子结构的hashmap。
use std::collections::HashMap;
#[derive(Debug)]
struct Node {
children: HashMap<i32, Node>,
}
impl Node {
fn no_immediate_children(&self) -> usize {
if self.children.is_empty() {
0
} else {
1 + self.children.into_iter().map(|(_, child)| child.no_immediate_children()).sum::<usize>()
}
}
}
我执行
no_immediate_children(&self)
以查找节点总数。然而,在
self.children
,rust会突出显示错误,因为:
cannot move out of `self.children` which is behind a shared reference
move occurs because `self.children` has type `std::collections::HashMap<i32, Node>`, which does not implement the `Copy` trait
我不知道丢失了什么。我刚开始生锈。我试着加上
&self.children...
但还是犯了同样的错误。