【发布时间】:2020-11-30 14:49:26
【问题描述】:
我想使用宏来生成函数的主体,但这样做需要访问本地范围内的变量:
macro_rules! generate_func {
($name:ident) => {
fn $name(param: i32) {
generate_body!()
}
};
}
macro_rules! generate_body {
() => {
println!("{}", ¶m);
}
}
generate_func!(foo);
error[E0425]: cannot find value `param` in this scope
--> src/lib.rs:11:25
|
11 | println!("{}", ¶m);
| ^^^^^ not found in this scope
...
15 | generate_func!(foo);
| -------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
这不起作用非常令人沮丧,因为我可以运行cargo-expand并看到生成的代码是有效的:
fn foo(param: i32) {
{
::std::io::_print(::core::fmt::Arguments::new_v1(&["", "\n"],
&match (&¶m,) {
(arg0,) =>
[::core::fmt::ArgumentV1::new(arg0,
::core::fmt::Display::fmt)],
}));
}
}
(由于println!,这有点乱,但你可以在那里看到有效的参考)
我什至可以将扩展复制并粘贴到我的源代码中,编译器会接受它。肯定有一些方法可以达到预期的结果吗?
似乎与以下内容大致相关:
- "cannot find value `a` in this scope" in Rust macro
- https://github.com/dtolnay/proc-macro-hack/issues/15
但我不能确定我是否遇到了完全相同的问题;我的情况似乎更基本。
【问题讨论】: