# Cartesian product using macros error

Hi all
I referred to this article and wrote a macro to calculate the Cartesian product, but I encountered a problem. When matching the 'merge' macro, it did not match the second rule. I would like to ask what the reason might be and how to fix it.
ref: Cartesian product using macros

``````#![recursion_limit = "256"]

macro_rules! merge {
(\$(
(\$a:literal, \$b:literal),
)*) => {
[
\$(concat!(\$a, \$b)),*
]
};
(\$(
((\$a:literal, \$b:literal), \$c:literal),
)*) => {
[
\$((\$a, \$b, \$c)),*
]
};
(\$(\$tt:tt)+)=>{stringify!(\$(\$tt)*)};
}

macro_rules! cartesian_impl {
(\$out:tt [\$(,)?] \$b:tt \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
\$cb!(\$out \$(\$cb_p)*)
};
(\$out:tt [\$a:expr, \$(\$at:tt)*] [\$(,)?] \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
cartesian_impl!{\$out [\$(\$at)*] \$init_b \$init_b, \$cb \$(\$cb_p)*}
};
([\$(\$out:tt)*] [\$a:expr, \$(\$at:tt)*] [\$b:expr, \$(\$bt:tt)*] \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
cartesian_impl!{[\$(\$out)* (\$a, \$b),] [\$a, \$(\$at)*] [\$(\$bt)*] \$init_b, \$cb \$(\$cb_p)*}
};
}

macro_rules! cartesian {
([\$(\$a:tt)*]) => {
merge!{\$(\$a)*}
};
([\$(\$a:tt)*], [\$(\$b:tt)*]) => {
cartesian_impl!{[] [\$(\$a)*,] [\$(\$b)*,] [\$(\$b)*,], cartesian}
};
([\$(\$a:tt)*], [\$(\$b:tt)*], \$(\$tail:tt)*) => {
cartesian_impl!{[] [\$(\$a)*,] [\$(\$b)*,] [\$(\$b)*,], cartesian , \$(\$tail)*}
};
}

fn main() {
let v = cartesian!([1, 2], [3, 4]);
println!("{:?}", v);
let v = cartesian! {[1, 2], [3, 4], [5, 6]};
println!("{:?}", v);
// let v = cartesian! {[1, 2], [3, 4], [5, 6], [7, 8]};
// println!("{:?}", v);
// let v = cartesian! {[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]};
// println!("{:?}", v);
// let v = cartesian!([a, b], [c, d]);
// println!("{:?}", v);
// let v = merge! { ((1, 3), 5), ((1, 3), 6), ((1, 4), 5), ((1, 4), 6), ((2, 3), 5), ((2, 3), 6),((2, 4), 5), ((2, 4), 6),};
// println!("{:?}", v);
}
``````

When uncommented, all of your example code compiles without errors and seems to produce the appropriate Cartesian products. What is the problem?

Thank you for your response. Below is my issue: after commenting out the 'tt' code, it cannot be compiled anymore.

``````#![recursion_limit = "256"]

macro_rules! merge {
(\$(
(\$a:literal, \$b:literal),
)*) => {
[
\$(concat!(\$a, \$b)),*
]
};
(\$(
((\$a:literal, \$b:literal), \$c:literal),
)*) => {
[
\$((\$a, \$b, \$c)),*
]
};
//(\$(\$tt:tt)+)=>{stringify!(\$(\$tt)*)};
}

macro_rules! cartesian_impl {
(\$out:tt [\$(,)?] \$b:tt \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
\$cb!(\$out \$(\$cb_p)*)
};
(\$out:tt [\$a:expr, \$(\$at:tt)*] [\$(,)?] \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
cartesian_impl!{\$out [\$(\$at)*] \$init_b \$init_b, \$cb \$(\$cb_p)*}
};
([\$(\$out:tt)*] [\$a:expr, \$(\$at:tt)*] [\$b:expr, \$(\$bt:tt)*] \$init_b:tt, \$cb:ident \$(\$cb_p:tt)*) => {
cartesian_impl!{[\$(\$out)* (\$a, \$b),] [\$a, \$(\$at)*] [\$(\$bt)*] \$init_b, \$cb \$(\$cb_p)*}
};
}

macro_rules! cartesian {
([\$(\$a:tt)*]) => {
merge!{\$(\$a)*}
};
([\$(\$a:tt)*], [\$(\$b:tt)*]) => {
cartesian_impl!{[] [\$(\$a)*,] [\$(\$b)*,] [\$(\$b)*,], cartesian}
};
([\$(\$a:tt)*], [\$(\$b:tt)*], \$(\$tail:tt)*) => {
cartesian_impl!{[] [\$(\$a)*,] [\$(\$b)*,] [\$(\$b)*,], cartesian , \$(\$tail)*}
};
}

fn main() {
let v = cartesian!([1, 2], [3, 4]);
println!("{:?}", v);
let v = cartesian! {[1, 2], [3, 4], [5, 6]};
println!("{:?}", v);
// let v = cartesian! {[1, 2], [3, 4], [5, 6], [7, 8]};
// println!("{:?}", v);
// let v = cartesian! {[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]};
// println!("{:?}", v);
// let v = cartesian!([a, b], [c, d]);
// println!("{:?}", v);
// let v = merge! { ((1, 3), 5), ((1, 3), 6), ((1, 4), 5), ((1, 4), 6), ((2, 3), 5), ((2, 3), 6),((2, 4), 5), ((2, 4), 6),};
// println!("{:?}", v);
}
``````

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.