If the idea is simply to iterate, I think a simpler solution is more valid.
Given the proposed modulation:
struct Node {
id: i64,
nodes: Vec<Node>,
}
We can simply build a function that takes a node (or a list of nodes, to support top level lists) and acts on that node. Then the function should follow to the list of nodes internal to the current node, repeating the process.
A flowchart representing this idea is set out below:
The proposed is then a recursive function. It could be represented as follows:
fn parse_nodes(nodes: &Vec<Node>) {
nodes.iter()
.map(|ref node| {
let id = node.id;
let len = node.nodes.len();
println!("id: {} - child nodes: {}", id, len);
parse_nodes(&node.nodes);
})
.count();
}
If we don’t care or can’t accept lists as the function’s initial argument, we can build the function as follows:
fn parse_node(node: &Node) {
let id = node.id;
let len = node.nodes.len();
println!("id: {} - child nodes: {}", id, len);
node.nodes
.iter()
.map(|ref node| parse_node(&node))
.count();
}
With these two solutions we have managed to solve the proposed problem of iterate a structure as exposed.
Both solutions could use parallelism elements to decrease the running time of the parser. This answer will not address this approach, however. For more information on this, a good reference is chapter 16 of the book The Rust Programming Language (link).
My same question asked in the English OS here https://stackoverflow.com/questions/47797743/how-to-correctly-iterate-all-records-of-a-multi-level-depth-structure-in-rust . Note the reply given by @Shepmaster in his last comment.
– galvares