How to elegantly permutate two vectors?


#1

I have two vectors:

    let attributes = ["acousticness",
                      "danceability",
                      "duration_ms",
                      "energy",
                      "instrumentalness",
                      "key",
                      "liveness",
                      "loudness",
                      "mode",
                      "popularity",
                      "speechiness",
                      "tempo",
                      "time_signature",
                      "valence"];
    let prefixs = ["min_", "max_", "target_"];

I want to get the permutations of this two vectors, so I do it with two iter():

for attribute in attributes {
    for prefix in prefixs {
        let param = prefix.to_owned() + attribute;
        println!("{:?}",param);
    }
}

Yes, it works. But I just wonder is there another elegant solution for my request ?


#2

Check out iproduct or cartesian_product from the itertools crate.

for (attribute, prefix) in iproduct!(attributes, prefixs) {
    /* ... */
}

#3

Note that this is not a permutation, it is a (direct) product. This should help with searching for alternative implementations.


#4

sounds great, thanks for your suggestin


#5

get it, it seems it’s my misunderstanding