Exercise 8: Nested Repetition

In this exercise, you will need to use nested repetition. That's where you write a repetition inside another one, for example, ( $( $( $val:expr ),+ );+ ) would let you specify at least one value, but seperate them with either ; and ,.

The only oddity about nested repetition is that you must ensure that you use metavariables in a context where it's clear you're only referring to one of them. In other words, the $val metavariable in the last paragraph must be used within a nested repetition.

Exercise 8: Nested Repetition

In this task, you will be building a macro to load a data structure with an adjacency list from a graph. As a refresher, graphs are data structures that describe how different nodes are connected.

Each will be a literal, and you will be specifying, for each node, which nodes it connects to. For example,

graph!{
    1 -> (2, 3, 4, 5);
    2 -> (1, 3);
    3 -> (2);
    4 -> ();
    5 -> (1, 2, 3);
}

should get translated into a Vec containing the pairs (1, 2), (1, 3), ... (2, 1), ... (5, 3).

You may not edit the main function, but it should eventually look like the following:

#[allow(clippy::vec_init_then_push)]
fn main() {
    let my_graph = {
        let mut vec = Vec::new();
        vec.push((1, 2));
        vec.push((1, 3));
        vec.push((1, 4));
        vec.push((1, 5));
        vec.push((2, 1));
        vec.push((2, 3));
        vec.push((3, 2));
        vec.push((5, 1));
        vec.push((5, 2));
        vec.push((5, 3));
        vec
    };
    print_vec(&my_graph);
}