Search code examples
rustscopemacroshygiene

How does the `quote` macro achieve interpolation?


How do the quote (declarative) and proc_quote (procedural) macros allow interpolation of local variables?

Since (declarative) macros are hygienic, I would expect that they wouldn't be able to access the environment they're called in.


Solution

  • Since (declarative) macros are hygienic, I would expect that they wouldn't be able to access the environment they're called in.

    Why though?

    vec![a, b, c]
    

    works fine.

    Macros being hygienic doesn't mean they can't access the environment they're called in (that would make them completely useless), it means they don't accidentally affect the environment they're called in, that is, the internal symbols of the macro will neither leak into nor conflict with environment symbols.

    For instance using C macros, the names inside and outside of the macro live in the exact same namespace, so it's possible to have collisions and odd side-effects.