#2D Grid Coordintaes Iteration Syntax?

2 messages · Page 1 of 1 (latest)

neon cosmos
#

I have a grid I've represented as a Vec<Vec<usize>>. I know that the length of all inner vectors is the same. Disregarding panics for now, is there a concise way to express an iterator through each element in the grid?

e.g. I want this:

(0..self.width())
  .flat_map(|x| {
    (0..self.height())
      .map(move |y| (x, y))
  })
  .for_each(move |(x, y)| {
    // do stuff
  });

But ideally as some more simple one-liner, like:

for (x, y) in /* something */ {
}
drowsy ore
#

if you like adding dependencies you can use itertools

for (x, y) in itertools::iproduct!(0..self.width(), 0..self.height()) {
}