I have the following Rust structure which has a HashMap to children structures.
use std::collections::HashMap; #[derive(Debug)] struct Node { children: HashMap<i32, Node>, } impl Node { fn no_children(&self) -> usize { if self.children.is_empty() { 1 } else { 1 + self .children .into_iter() .map(|(_, child)| child.no_children()) .sum::<usize>() } } } I implemented no_children(&self) to find the total number of nodes. However, under self.children, Rust highlights an error because:
error[E0507]: cannot move out of `self.children` which is behind a shared reference --> src/lib.rs:13:17 | 13 | 1 + self | _________________^ 14 | | .children | |_________________________^ move occurs because `self.children` has type `std::collections::HashMap<i32, Node>`, which does not implement the `Copy` trait I am not sure what is missing. I have tried adding &self.children... but still got the same error.
no_immediate_childrencounts all the children recursively, not just the immediate ones…