I have a tree of Nodes where each node might have a name. I'm trying to
convert the tree into a HashMap of named nodes, but my code is getting
extremely complex due to the fact that I cannot pass &mut HashMap around.
let mut named_nodes = HashMap::new();
let nodes = vec!( ... );
named_nodes = self.collect_node_names(&named_nodes, &nodes);
println!('{}', named_nodes);
fn collect_node_names(&self, map: &HashMap<String, Gc<node::Node>>,
nodes: &Vec<Gc<node::Node>>) -> HashMap<String, Gc<node::Node>> {
let mut local_map: HashMap<String, Gc<node::Node>> = HashMap::new();
for (k,v) in map.iter() {
local_map.insert(k.clone(), *v);
}
for n in nodes.iter() {
for (k,v) in self.collect_node_names(&local_map, &n.subnodes).iter() {
local_map.insert(k.clone(), *v);
}
match n.name {
Some(ref name) => {
if local_map.contains_key(name) {
} else {
local_map.insert(name.clone(), *n);
}
},
None => (),
}
}
local_map
}
this one works, but it's bloated and slow. Any hints on how to improve the
code?
--
Sincerely,
Vladimir "Farcaller" Pouzanov
http://farcaller.net/
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev