在宏规则中!转录器,嵌套重复处理不正确。这工作正常:
macro_rules! demo_macro {
($op:tt; $($arg:tt),*) {
($($op * $arg),*)
}
}
fn main() {
println!("{?:}", demo_macro!(2; 1,2,3));
}
和输出(2, 4, 6)
,但这不能编译:
macro_rules! demo_macro {
([$($op:tt)*] $($arg:tt),*) {
($($($op)* $arg),*)
}
}
fn main() {
println!("{?:}", demo_macro!([2*] 1,2,3));
}
并导致此错误消息:
error: meta-variable `op` repeats 2 times, but `arg` repeats 3 times
--> src/main.rs:3:11
|
3 | ($($($op)* $arg),*)
| ^^^^^^^^^^^^^^
如果我改变1,2,3
to 1,2
因此重复计数匹配,我收到此错误消息:
error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
--> src/main.rs:3:13
|
3 | ($($($op)* $arg),*)
| ^^^^^
知道如何制作demo_macro!([2*] 1,2,3)
导致(2, 4, 6)
?