Dynamic Nested Multi-Dimensional Arrays in Rust

57 Views Asked by At

I am new to Rust but recently came across a problem I don't know how to solve. It's to work with nested (and multi-dimensional) value-key pair arrays in Rust that is dynamically generated based on string splitting.

The sample dataset looks something like the example below:

Species | Category
Dog     | Eukaryota, Animalia, Chordata, Mammalia, Carnivora, Canidae, Canis, C. familiaris
Cat     | Eukaryota, Animalia, Chordata, Mammalia, Carnivora, Feliformia, Felidae, Felinae, Felis, F. catus
Bear    | Eukaryota, Animalia, Chordata, Mammalia, Carnivora, Ursoidea, Ursidae, Ursus
...

The goal would be to split the comma delimitation and a create a map or vector. Essentially, creating "layers" of nested keys (either as a vector array or a key to a final value).

From my understanding, Rust has a crate called serde_json which can be used to create key-value pairings like so:

let mut array = Map::new();
for (k, v) in data.into_iter() {
    array.insert(k, Value::String(v));
}

As for comma delimited string splitting, it might look something like this:

let categories = "a, b, c, d, e, f".split(", ");
let category_data = categories.collect::<Vec<&str>>()

However, the end goal would be to create a recursively nested map or vector that follows the Category column for the array which can ultimately be serialised to a json output. How would this be implemented in Rust?

In addition, while we might know the number of rows in the sample dataset, isn't it quite resource intensive to calculate all the "comma-delimited layers" in the Category column to know the final size of the array as required by Rust's memory safe design to "initialize" an array by a defined or specified size? Would this need to specifically implemented as way to know the maximum number of layers in order to be doable? Or can we implement an infinitely nested multi-dimensional array without having to specify or initialise a defined map or vector size?

For further reference, in PHP, this might be implemented so:

$output_array = array();

foreach($data_rows as $data_row) {
    $temp =& $output_array;

    foreach(explode(', ', $data_row["Category"]) as $key) {
        $temp =& $temp[$key];
    }

    // Check if array is already initialized, if not create new array with new data
    if(!isset($temp)) {
        $temp = array($data_row["Species"]);
    } else {
        array_push($temp, $data_row["Species"]);
    }
}

How would a similar solution like this be implemented in Rust?

0

There are 0 best solutions below