macro_rules! call_on_self {
($F:ident) => {
self.$F()
}
}
struct F;
impl F {
fn dummy(&self) {}
fn test(&self) {
call_on_self!(dummy);
}
}
上述方案无法运行 (Playground):
error[E0424]: expected value, found module `self`
--> src/lib.rs:3:9
|
3 | self.$F()
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
...
11 | call_on_self!(dummy);
| --------------------- in this macro invocation
我不明白为什么这个不能工作:宏是在有self
的方法中调用的!这可能怎么可能?我是否应该将self
传递给宏,否则宏将无法解析self
?
我正在使用rustc 1.19.0-nightly。
gen_match
仅被test
使用,您可以将macro_rules!
移动到函数内部。 - kennytm