TL;DR — Quick Summary

cargo-expand shows the result of macro expansion in Rust code. Debug derive macros, procedural macros, and macro_rules with full output.

cargo-expand reveals what Rust macros actually generate. Essential for debugging derive and proc macros.

Installation

# Install cargo-expand
cargo install cargo-expand

# Requires nightly Rust
rustup install nightly

Usage

# Expand all macros in the crate
cargo expand

# Expand a specific module
cargo expand models

# Expand a specific item
cargo expand models::User

# With syntax highlighting
cargo expand | bat -l rs

# Expand tests
cargo expand --tests

Example

// Input
#[derive(Debug, Clone)]
struct User {
    name: String,
    age: u32,
}

cargo-expand shows the generated Debug and Clone implementations.

Summary

  • cargo-expand: see Rust macro expansion output
  • Debug derive macros, proc macros, macro_rules
  • Expand specific items or entire crate
  • Requires nightly Rust
  • Pipe to bat for colorized output