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 construct a function that receives 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 explained below:
Theproposedoneisthenarecursivefunction.Itcouldberepresentedasfollows:
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 do not care or can not accept lists as the initial argument of the function, we can construct 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 can solve the proposed problem of iterating a structure like the one exposed.
Both solutions could use parallelism elements to shorten the parser runtime. This answer will not address this approach, however. For more information, a good reference is Chapter 16 of The Rust Programming Language ( link ).