Need a solution

Hello,

I don't know exactly how to formulate this. This is what I want

let iter = (1..3).some_method((1..3)).iter();

assert_eq! (iter.next(), Some((&1, &1)));
assert_eq! (iter.next(), Some((&1, &2)));
assert_eq! (iter.next(), Some((&1, &3)));

assert_eq! (iter.next(), Some((&2, &1)));
assert_eq! (iter.next(), Some((&2, &2)));
assert_eq! (iter.next(), Some((&2, &3)));

assert_eq! (iter.next(), Some((&3, &1)));
assert_eq! (iter.next(), Some((&3, &2)));
assert_eq! (iter.next(), Some((&3, &3)));

I just want to know what should be that method is. And it doesn't need to be exactly in the tuple wrapped in Option. I just want to be like in that matrix pattern. That's enough for me. please help me out here

Hi,

seems you are looking for the ZIP function: rust doc zip

let a1 = [1, 2, 3];
let a2 = [4, 5, 6];

let mut iter = a1.iter().zip(a2.iter());

assert_eq!(iter.next(), Some((&1, &4)));
assert_eq!(iter.next(), Some((&2, &5)));
assert_eq!(iter.next(), Some((&3, &6)));
assert_eq!(iter.next(), None);

No, I don't want like zip function. I want like this
for range 1..=3 and 1..=3
I should get
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
but, anyway, thanks for the swift reply

This is called a cartesian product.

5 Likes

Ahhh, yes. I had completely forgot about that. Thanks for reminding this. And thanks for the help :smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.