我正在尝试实现这样的目标(简化):
macro_rules! atest {
($closure:tt) => {
let x = 5;
println!("Result is {}", $closure())
};
}
fn main() {
//let x = 50;
atest!((|| 5 + x));
}
它不起作用,因为宏的参数atest
在宏评估之前由编译器考虑:
error[E0425]: cannot find value `x` in this scope
--> src/main.rs:10:20
|
10 | atest!((|| 5 + x));
| ^ not found in this scope
有可能完成这项工作吗?我的理解是宏在编译之前被扩展。