Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

cargo-expand

> 包管理
Open source

Subcommand to show result of macro expansion

3.1K stars0 likes2 views
WebsiteGitHub

About

Subcommand to show result of macro expansion

cargo-expand

Once installed, the following command prints out the result of macro expansion and #[derive] expansion applied to the current crate.

bash
$ cargo expand

This is a wrapper around the more verbose compiler command:

bash
$ cargo rustc --profile=check -- -Zunpretty=expanded

Installation

Install with cargo install cargo-expand.

This command optionally uses rustfmt to format the expanded output. The resulting code is typically much more readable than what you get from the compiler. If rustfmt is not available, the expanded code is not formatted. Install rustfmt with rustup component add rustfmt.

Example

$ cat src/main.rs

rust
#[derive(Debug)]
struct S;

fn main() {
    println!("{:?}", S);
}

$ cargo expand

…

Options

See cargo expand --help for a complete list of options, most of which are consistent with other Cargo subcommands. Here are a few that are common in the context of cargo expand.

To expand a particular test target:

$ cargo expand --test test_something

To expand without rustfmt:

$ cargo expand --ugly

To expand a specific module or type or function only:

$ cargo expand path::to::module

cargo expand punctuated::printing cargo expand token::FatArrow

Configuration

The cargo expand command reads the [expand] section of $CARGO_HOME/config.toml if there is one (usually ~/.cargo/config.toml).

Set the default syntax highlighting theme with the theme setting:

toml
[expand]
theme = "TwoDark"

Run cargo expand --themes or bat --list-themes to print a list of available themes. Use theme = "none" to disable coloring.

Change the default coloring disposition (normally auto) with the color setting:

toml
[expand]
color = "always"

Enable paging of the output with the pager setting:

toml
[expand]
pager = true

Disclaimer

Be aware that macro expansion to text is a lossy process. This is a debugging aid only. There should be no expectation that the expanded code can be compiled successfully, nor that if it compiles then it behaves the same as the original code.

For instance the following function returns 3 when compiled ordinarily by Rust but the expanded code compiles and returns 4.

rust
fn f() -> i32 {
    let x = 1;

    macro_rules! first_x {
        () => { x }
    }

    let x = 2;

    x + first_x!()
}

Refer to The Book for more on the considerations around macro hygiene.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Issues· 10 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustcargo

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image