#iterator #macro #no-std #cartesian-iterator

no-std cartesian

QOL macro that creates the cartesian product of multiple iterators

4 releases (2 breaking)

0.3.0 Mar 21, 2025
0.2.1 Mar 30, 2021
0.2.0 Mar 30, 2021
0.1.0 Mar 30, 2021

#313 in Rust patterns

Download history 843/week @ 2024-12-14 122/week @ 2024-12-21 357/week @ 2024-12-28 497/week @ 2025-01-04 1227/week @ 2025-01-11 981/week @ 2025-01-18 1022/week @ 2025-01-25 1436/week @ 2025-02-01 1181/week @ 2025-02-08 1850/week @ 2025-02-15 1855/week @ 2025-02-22 1737/week @ 2025-03-01 1768/week @ 2025-03-08 1798/week @ 2025-03-15 1821/week @ 2025-03-22 1732/week @ 2025-03-29

7,377 downloads per month
Used in 2 crates (via rs2glsl-macros)

MIT license

8KB
103 lines

cartesian-rs

A macro for combining iterators to the cartesian product. Using the macro it is possible to write code compacter, and use less indention space.

Writing one for-loop and iterating through the cartesian product of multiple iterators is similar to nesting for loops, in each loop iterating through one of the iterators. However, the semantics of break is changed.

Example

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);

License

This package is licensed under the MIT license.

No runtime deps