Crate cartesian[][src]

Provides a quality of life macro cartesian! to simplify certain loops.

The macro takes up to 26 iterators as arguments and creates the cartesian product iterator over all input iterators, kind of like nested for loops.

It behaves the same as nested for loops and brings the advantage of being more compact, and simplifies breaking and continuing.


use cartesian::*;

let mut volume_grid = vec![vec![vec![0; 10]; 10]; 10];
for (x, y, z) in cartesian!(0..10, 0..10, 0..10) {
    volume_grid[x][y][z] = x * y + z;
let (m, n, p) = (3, 3, 1);

let mut id = vec![vec![0; n]; m];
for (i, j) in cartesian!(0..m, 0..n) {
    id[i][j] = (i == j) as u32;

let col_vec = vec![vec![1], vec![2], vec![4]];

let mut res = vec![vec![0; p]; m];

for (i, j, k) in cartesian!(0..m, 0..n, 0..p) {
    res[i][k] += id[i][j] * col_vec[j][k];

assert_eq!(col_vec, res);



The macro this is all about.



Helper trait implemented for all tuples up to 26 elements to prepend a value to produce a longer tuple