Debugging macros
When developing complex macros, most of the time you need ways to analyze how your code expands to the inputs you gave to the macro. You can always use println!
or panic!
at the places you want to see the generated code, but it's a very crude way to debug it. There's are better way, though. The Rust community provides us with a subcommand called cargo-expand
. This subcommand was developed by David Tonlay at https://github.com/dtolnay/cargo-expand, who is also the author of the syn
and quote
crates. This command internally calls the nightly compiler flagĀ -Zunstable-options --pretty=expanded
, but the design of the subcommand was done in such a way that it doesn't require you to manually switch to the nightly tool chain as it finds and switches to it automatically. To demonstrate this command, we'll take the example of our IntoMap
derive macro and observe what code it generated for us. By switching into the directory and running cargo expand
, we get the following output:
As...