我想实现类似于这样的效果(简化版):
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
这是否有可能实现?我的理解是宏在编译之前就被展开了。