【发布时间】:2017-10-22 13:25:28
【问题描述】:
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!移动到函数内部。
标签: rust rust-macros