【发布时间】:2018-03-28 05:09:35
【问题描述】:
我希望编写一个 Rust 宏,它将其整个参数转发给第二个宏——即使该参数包含令人兴奋的括号。
这是我迄今为止尝试过的:
macro_rules! parse {
(done) => (println!("done!"));
(if ($cond:tt) {$then:tt}) => (println!("if! "); parse!($cond); parse($then));
}
macro_rules! forward {
($($e:tt)*) => (parse!($($e)*; done));
}
fn main() {
forward!(if (done) {done} );
}
这不起作用,并产生错误:
error: no rules expected the token `if`
我在这里做错了什么?
编辑:除了简单地将参数转发给forward,我还希望将标记; done“粘贴”到forward 参数的末尾。有没有办法在保持这种行为的同时完成这项工作?
【问题讨论】: