Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Rust macro output [duplicate]

Tags:

macros

rust

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);
}

1 Answers

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); }

Update

With the 2021 version the command changed to (thanks @at54321):

rustc -Zunpretty=expanded FILENAME.rs

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!