I've been following the book: Push-down Accumulation.
One macro in the book is defined as:
macro_rules! init_array {
(@accum (0, $_e:expr) -> ($($body:tt)*))
=> {init_array!(@as_expr [$($body)*])};
(@accum (1, $e:expr) -> ($($body:tt)*))
=> {init_array!(@accum (0, $e) -> ($($body)* $e,))};
(@accum (2, $e:expr) -> ($($body:tt)*))
=> {init_array!(@accum (1, $e) -> ($($body)* $e,))};
(@accum (3, $e:expr) -> ($($body:tt)*))
=> {init_array!(@accum (2, $e) -> ($($body)* $e,))};
(@as_expr $e:expr) => {$e};
[$e:expr; $n:tt] => {
{
let e = $e;
init_array!(@accum ($n, e.clone()) -> ())
}
};
}
What I don't understand is why the macro cannot be defined as recursion. I tested writing it recursion style but hit the recursion limit directly (which in my opinion shouldn't be so) with the following code let array = init_array![String::from("Hello"); 3]
.
Here is the recursion example:
macro_rules! init_array {
(@accum (0, $_e:expr) -> ($($body:tt)*)) => {
init_array!(@as_expr [$($body)*])
};
(@accum ($n:expr, $e:expr) -> ($($body:tt)*)) => {
{
let _n = $n - 1;
init_array!(@accum (_n, $e) -> ($($body)* $e,))
}
};
(@as_expr $e:expr) => {$e};
[$e:expr; $n:tt] => {
{
let e = $e;
init_array!(@accum ($n, e) -> ())
}
};
}
Why is this?