A Cargo subcommand to show the result of Rust macro expansion and #[derive] expansion for debugging.
cargo-expand is a Cargo subcommand that reveals the expanded source code after Rust macros and `#[derive]` attributes have been processed. It helps developers debug complex macros by showing the actual generated code that the compiler sees, formatted for readability. The tool wraps verbose compiler commands into a simple interface.
Rust developers working with macros, procedural macros, or derive macros who need to inspect generated code for debugging or understanding macro behavior.
Developers choose cargo-expand because it provides a clean, formatted view of macro-expanded code with rustfmt integration and syntax highlighting, making it far more readable than raw compiler output. It's specifically designed as a debugging aid that integrates seamlessly into the Cargo workflow.
Subcommand to show result of macro expansion
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Integrates with rustfmt to format expanded code, making it much more readable than raw compiler output, as shown in the example with structured Debug implementation.
Allows expansion of specific modules, types, or functions, enabling focused debugging without overwhelming output, supported by options like `cargo expand path::to::module`.
Supports syntax highlighting themes and pager settings through Cargo config files, enhancing visualization for different preferences, as detailed in the Configuration section.
Installs as a Cargo subcommand and uses familiar Cargo options, making it easy to integrate into existing workflows without complex setup.
As per the disclaimer, macro expansion to text is lossy, and expanded code may not compile or behave identically to the original, limiting its use for verification.
For formatted output, requires rustfmt to be installed separately; without it, the code is unformatted and less readable, adding an extra step for users.
Lacks GUI or IDE integration, which might be less convenient for developers accustomed to visual debugging environments or real-time feedback.