Macro expansion, type checking and variable shadowing error


I am relatively new to rust and am currently trying to translate a structure to rust - but am running into what feels like a bug but probably is a lack of understanding of macros.

The issue is best explained in Rust Playground ( which is a commented "minimal example".

In short I am trying to write a macro that will let me (efficiently and with clear code) run a closure over a set of curves.

Ref the link above I have two questions:

  1. Why does the original code not compile - is it because the type checker is run before the macro expansion?
  2. If commenting out lines 72 & 73 -> why is the result not as expected? Surely the inner "x" should here shadow the outer one?

Thanks for any pointers

The problem is macro hygiene which ensures that variables defined inside a macro are in a different namespace from variables outside the macro. Consider making the variable name an input to the macro?

For exapmle, try this


Macro hygiene is a good key-word for me to explore furhter.

Indeed your example is a good way to work around it - thanks.

Macro expansion happens before:

  1. Type checking
  2. Borrow checking

In fact, given the way it works, macro expansion is one of the earliest steps of the compilation process, probably right after parsing.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.