3

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);
}
tversteeg
  • 4,717
  • 10
  • 42
  • 77

1 Answers1

10

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
tversteeg
  • 4,717
  • 10
  • 42
  • 77
  • 3
    It's 2021 and things have changed a bit. Now you need to use `rustc -Zunpretty=expanded some.rs` – at54321 Nov 03 '21 at 22:04