为什么调用闭包与在宏中定义的函数会产生不同的结果?

时间:2019-07-31 22:18:22

标签: compiler-errors rust closures

这是一些库代码的简化示例。由于某种原因,函数m在该范围内是可调用的,而闭包n却不是:

pub trait TestTrait {
    fn testFunc();
}

macro_rules! custom {
    ($body:expr) => {
        fn testFunc() {
            fn m() {}
            let n = || {};
            $body
        }
    };
}

macro_rules! int_custom {
    () => {
        impl TestTrait for i32 {
            custom!({
                m();
                n();
            });
        }
    };
}

int_custom!();

fn main() {
    println!("Test");
}
error[E0425]: cannot find function `n` in this scope
  --> src/main.rs:20:17
   |
20 |                 n();
   |                 ^ not found in this scope
...
26 | int_custom!();
   | -------------- in this macro invocation

为什么会这样?

0 个答案:

没有答案