I was trying to implement something like a visitor pattern. I wrapped the output into a struct:
struct DumpGraphviz {
out: Box<dyn Write>,
id: u32
}
so I can create a object visit
with type DumpGraphviz
and pass it into function.
When I try to create a lot of output using map, I have to write:
// fn dump_node(&self, visit: &mut DumpGraphviz) -> u32;
// let v = vector of some data
let children : Vec<_> = v.iter().map(|n| n.dump_node(visit)).collect();
visit.dump_children("VARIABLE_DECL_LIST_NODE", &children)
My question is that, why I cannot put these into one line, like:
// let v = vector of some data
visit.dump_children("VARIABLE_DECL_LIST_NODE",
&v.iter().map(|n| n.dump_node(visit)).collect::<Vec<_>>())
Compiler will complain that visit is first borrow when calling dump_children, and second borrow occurred at dump_node(visit)
This seems a little weird, since preparing the argument of dump_children should not conflict with the real call.,Can somebody explain why compiler think the code has problem?