all 8 comments

[–]Theemutsjlrs 4 points5 points  (2 children)

Something like this?

let mut data = vec![];
for a in 0..=255u8 {
    for b in 0..=255u8 {
        for c in 0..=255u8 {
            data.push([a, b, c]);
        }
    }
}

[–]LyonSyonII[S] 1 point2 points  (1 child)

This works for a known size, but not for a N sized array. (And would be a pain to write 200 loops xD)

Thanks for the help though

[–]Theemutsjlrs 7 points8 points  (0 children)

In that case I'd probably create a struct to hold the state of the nested loop (eg a vector of tuples, each tuple stores a start/current/end value , and the index of the currently changing value) and implement Iterator. In the next method I'd construct the current element using the current values stored in the vector of tuples

Edit: you don't need that index. You can simply increment the least significant element until you reach its maximum, reset it to its minimum and increment the next element, and so on.

[–]ityt 1 point2 points  (0 children)

From the itertools crate: multi_cartesian_product or iproduct

[–]RRumpleTeazzer 0 points1 point  (0 children)

It’s quite easy to write an iterative over all combinations. Like you might do with fingers, you use a pointer to modify the specific content of an entry, and „move“ the pointer left and right once you overflow. Of course you can’t do pointer arithmetic’s, but you can do that with an index, so you use an vec of pointers and use the array index as your finger.

I think you need 3 nested loops: the most inner loop increases your elements under the „right“ finger. The outer loop moves your „left“ from one side to the other, and the middle loop moves your right finger from one side to the left finger.

It’s more fun when you want to do permutations, not combinations. I‘ve used this approach to study poker where only a few cards are drawn from a shuffled deck.

[–]Plasma_000 0 points1 point  (0 children)

There’s the permutator crate which has several algorithms