1

This is a simplified example from some library code. For some reason, the function m is callable in that scope but the closure n isn't:

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

Why is this the case?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
m1cky22
  • 208
  • 1
  • 6
  • Don't put line numbers in your code snippets. Code should be runnable, and random numbers in the source do not help that goal. – Shepmaster Aug 01 '19 at 02:56
  • By the way, idiomatic Rust uses `snake_case` for variables, methods, macros, fields and modules; `UpperCamelCase` for types and enum variants; and `SCREAMING_SNAKE_CASE` for statics and constants. – Shepmaster Aug 01 '19 at 02:57
  • Your question is the same as the linked duplicate with `s/constant/function/`. – Shepmaster Aug 01 '19 at 03:00

0 Answers0