What is proc-macro2 and when to use it
masterproc-macro2 is a wrapper around the compiler's proc_macro crate. It provides a way to use procedural macro types in contexts where the standard proc_macro crate is unavailable or unsuitable.
Use proc-macro2 to:
- Use macro types in non-macro contexts: Types from
proc_macrocan only exist inside a procedural macro.proc_macro2types can be used inbuild.rs,main.rs, or any other non-macro code. This allows libraries likesynandquoteto be used outside of macros. - Enable unit testing: Because
proc_macrocan only be executed within the compiler's macro expansion process, code using it cannot be unit tested. Implementing macro logic usingproc_macro2allows you to test components in isolation.