I have a recursive type which doesn't implement copy
struct Node {
board: Board,
children: Option<Vec<Node>>,
}
I have a Vector of these nodes, I want to call a method on each, and then pass them on to another function.
fn foo() {
let mut nodes: Vec<Node> = get_nodes();
for i in 0..nodes.len() {
nodes[i].grow();
}
pick_node(nodes);
}
This works, but frustratingly
fn foo() {
let nodes: Vec<Node> = get_nodes();
for mut node in nodes {
node.grow();
}
pick_node(nodes);
}
causes an error because "nodes
moved due to this implicit call to .into_iter()
"
Is there a version of for _ in _
that just gets me references to each member of the vector in turn, without moving or copying, like I get when I use a range like in the first version? It's an issue that comes up a lot for me, and it's been hard to find an answer for some reason. Like Clippy says, the iterator is probably faster, and clearer, and just seems idiomatic.