我正在尝试编写一个为我编写函数的宏。请记住,我删除了很多不相关的细节,所以它可能看起来有点毫无意义。 我的宏看起来像这样:
macro_rules! command {
// Vars
(@vars, $var: ident: ?$type: ty, $($t:tt)*) => {
$var: Option<&$type>, command!(@vars, $($t)*)
};
(@vars, $var: ident: $type: ty, $($t:tt)*) => {
$var: &$type, command!(@vars, $($t)*)
};
(@vars, ) => {};
// Main
($name: ident ($($t:tt)*) $body: block) => {
fn $name(command!(@vars, $($t)*)) $body
};
}
我像这样调用它:
command!(lol1_command(text: String, t: ?String,) {
println!("test")
});
这给了我一个错误。当我使用 rust 分析仪扩展它时,我得到了这个有效的
fn lol1_command(text: &String, t: Option<&String>) {
println!("cool");
}
我得到的编译错误是
error: expected one of ':' or '|', found ')'
它推荐了我fn $name(_: command!(@ vars, text : String, t : ? String,)) $body
在我看来,它不会递归地扩展 command
,并且 insted 认为宏调用是函数的参数。到底是怎么回事?我很困惑。预先感谢。
我尝试在宏中移动一些东西,以尝试在调用 vars 宏后移动函数生成,但无济于事。