我希望使用宏来生成函数体,但为了做到这一点,它们需要访问局部范围内的变量:
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(Rust 宏中找不到值 `a`)
- https://github.com/dtolnay/proc-macro-hack/issues/15
但我不能确定我是否有完全相同的问题; 我的情况似乎更基础些。