How do I see the expanded macro code that's causing my compile error?

cargo rustc --profile=check -- -Zunpretty=expanded, but a more concise alternative is the cargo-expand crate. It provides a Cargo subcommand cargo expand which prints the result of macro expansion. It also passes the expanded code through rustfmt which generally results in much more readable code than the default output from rustc.

Install by running cargo install cargo-expand.


Yes, you can pass a special flag to rustc, called --pretty=expanded:

% cat test.rs
fn main() {
    println!("Hello world");
}
% rustc -Z unstable-options --pretty=expanded test.rs
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate "std" as std;
fn main() {
    ::std::old_io::stdio::println_args(::std::fmt::Arguments::new_v1({
                                                                         static __STATIC_FMTSTR:
                                                                                &'static [&'static str]
                                                                                =
                                                                             &["Hello world"];
                                                                         __STATIC_FMTSTR
                                                                     },
                                                                     &match ()
                                                                          {
                                                                          ()
                                                                          =>
                                                                          [],
                                                                      }));
}

You need to allow it first, however, by passing -Z unstable-options.

Since Rust 1.1 you can pass these arguments to Cargo, like this:

cargo rustc -- -Z unstable-options --pretty=expanded

Starting with nightly-2021-07-28, one must pass -Zunpretty=expanded instead of -Zunstable-options --pretty=expanded, like this:

% rustc -Zunpretty=expanded test.rs

or:

% cargo rustc -- -Zunpretty=expanded

Relevant rustc commits

The --pretty argument was removed from nightly-2021-07-28 via this commit. Support for -Zunpretty=expanded was added to nightly-2018-01-24 via this commit.