How can I see the code output for expanded Rust macros?
For example I have this snippet:
macro_rules! five_times {
($x:expr) => (5 * $x);
}
fn main() {
let a = five_times!(2 + 3);
}
And I want to see something like this:
fn main() {
let a = 5 * (2 + 3);
}
When using nightly Rust you can use the following command on a source file:
rustc --pretty expanded -Z unstable-options FILENAME.rs
This will print the following output:
macro_rules! five_times(( $ x : expr ) => ( 5 * $ x ) ;);
fn main() { let a = 5 * (2 + 3); }
With the 2021 version the command changed to (thanks @at54321):
rustc -Zunpretty=expanded FILENAME.rs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With