cargo-expand to inspect macro expansions

Marcus Chen Jan 2026
1 tab
# Install
cargo install cargo-expand

# Expand entire crate
cargo expand

# Expand specific function
cargo expand module::my_function
1 file · bash Explain with highlit

cargo expand is a subcommand that shows the output of macro expansion. It's invaluable for debugging derive macros, understanding what declarative macros produce, or learning how async functions desugar. I run cargo expand --lib to see the entire crate or cargo expand module::function for a specific item. The output is fully-expanded Rust code with all macros resolved. This helps when macro errors are cryptic or when you want to understand the generated code. It requires nightly Rust but is a must-have tool for macro development. Combined with rust-analyzer's inline expansion, it makes working with macros much less mysterious.