Skip to content

cartesian_product

Returns every pair (x, y) with x from a and y from b, in row-major order.

For more than two inputs, nest the calls.

use helpers4::array::cartesian_product;

Cargo feature array (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features array

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["array"] }
pub fn cartesian_product<A: Clone, B: Clone>(a: &[A], b: &[B]) -> Vec<(A, B)>
ParameterTypeDescription
a&[A]The first slice.
b&[B]The second slice.

Vec<(A, B)>

use helpers4::array::cartesian_product;

assert_eq!(
    cartesian_product(&[1, 2], &['a', 'b']),
    vec![(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
);

src/array/cartesian_product.rs