Published on

What is cargo expand?

Authors
A literary style image of the early earth expanding from the universe's ether.

Rust macros are like magic—powerful but sometimes mystifying. They save time and reduce boilerplate, but when something goes wrong or you need to understand exactly what’s happening under the hood, they can feel like an inscrutable black box.

Enter cargo-expand

This little gem is a tool designed to lift the curtain, showing you what macros (and those handy #[derive] attributes) expand into during compilation. Whether you’re debugging, learning, or just plain curious, cargo-expand is an invaluable addition to your developer toolbox.

What exactly is it?

At its core, cargo-expand displays the fully expanded Rust source code generated by macros. You can see how your macro invocations transform into actual Rust code—without the need to decipher it mentally or guess at the compiler's logic.


Setting things up

Before you dive into using cargo-expand, you’ll need the nightly Rust toolchain. Why? Because cargo-expand relies on unstable features that only nightly supports.

Here’s how you get started:

  1. Install the nightly toolchain:

    rustup toolchain install nightly --allow-downgrade
    

    No worries, this doesn't force your projects into the nightly build!

  2. Install cargo-expand itself:

    cargo +nightly install cargo-expand
    
  3. (Optional, but do it) Add rustfmt for clean, readable output:

    rustup component add rustfmt
    

Using it

Once installed, using cargo-expand is as simple as navigating to your project directory and running:

cargo +nightly expand

This runs the command with the nightly version

The tool will spit out the expanded code for your project, allowing you to examine exactly what’s happening under the hood.

For example, imagine we have a User struct:

#[derive(Debug)]
struct User {
    id: u32,
    uuid: String,
    name: String,
    address: String,
}

Running cargo-expand will show you the compiler-generated implementation of Debug for User.

This makes it much easier to understand what’s going on when something unexpected happens or when you’re building your own macros and need to see the results.

Why it's so great

If you’ve ever stared at a cryptic compiler error involving a macro and thought, “What on earth is this doing?”, cargo-expand is your flashlight in the dark. It’s also a fantastic learning tool if you’re just getting started with Rust macros or working on your first custom procedural macro.

The best part? It integrates seamlessly into your existing Rust workflow. No need to set up complex debugging tools—just install it, run it, and gain immediate insight.

Dive in deeper

For more information, check out the cargo-expand GitHub repository. And if you’re jumping into the world of macros, don’t forget to pair this tool with the excellent Rust documentation on macros.

In summary

Rust macros often seem magical, but with tools like cargo-expand, we can quickly discover for ourselves that they’re another tool for practicing precision engineering.

So jump in and take a magnifying glass to your macros!

Subscribe to the newsletter