랭킹으로 돌아가기

dtolnay/cargo-expand

Rust

Subcommand to show result of macro expansion

cargo
스타 성장
스타
3.1k
포크
75
주간 성장
이슈
10
1k2k3k
2016년 6월2019년 10월2023년 3월2026년 7월
아티팩트crates.iocargo add cargo-expand
README

cargo-expand

github crates.io build status

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

$ cargo expand

This is a wrapper around the more verbose compiler command:

$ 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

#[derive(Debug)]
struct S;

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

$ cargo expand

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
struct S;
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::fmt::Debug for S {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {
            S => {
                let mut debug_trait_builder = f.debug_tuple("S");
                debug_trait_builder.finish()
            }
        }
    }
}
fn main() {
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(
            &["", "\n"],
            &match (&S,) {
                (arg0,) => [::core::fmt::ArgumentV1::new(arg0, ::core::fmt::Debug::fmt)],
            },
        ));
    };
}

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:

[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:

[expand]
color = "always"

Enable paging of the output with the pager setting:

[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.

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.
관련 저장소
rust-lang/cargo

The Rust package manager

Rustcrates.ioApache License 2.0rustpackage-manager
doc.rust-lang.org/cargo
15.3k3k
johnthagen/min-sized-rust

🦀 How to minimize Rust binary size 📦 https://github.com/johnthagen/min-sized-rust

Rustcrates.ioMIT Licenserustbinary-size
9.8k247
cross-rs/cross

“Zero setup” cross compilation and “cross testing” of Rust crates

Rustcrates.ioApache License 2.0rustcross-compilation
8.3k460
RubyMetric/chsrc

chsrc 全平台通用换源工具与框架. Change Source everywhere for every software

CGNU General Public License v3.0gemlinux
chsrc.run
6.8k277
graphql-rust/juniper

GraphQL server library for Rust

Rustcrates.ioOthergraphqlrust
6k443
killercup/cargo-edit

A utility for managing cargo dependencies from the command line.

Rustcrates.ioMIT Licenserustcargo
killercup.github.io/cargo-edit/
3.4k165
Canop/bacon

background code checker

Rustcrates.ioGNU Affero General Public License v3.0rustterminal
dystroy.org/bacon
3.3k121
xd009642/tarpaulin

A code coverage tool for Rust projects

Rustcrates.ioApache License 2.0cargocoverage-report
crates.io/crates/cargo-tarpaulin
3k197
sagiegurari/cargo-make

Rust task runner and build tool.

Rustcrates.ioApache License 2.0buildbuild-tool
sagiegurari.github.io/cargo-make/
2.9k147
watchexec/cargo-watch

Watches over your Cargo project's source.

Rustcrates.ioCreative Commons Zero v1.0 Universalrustcargo
2.9k85
LukeMathWalker/cargo-chef

A cargo-subcommand to speed up Rust Docker builds using Docker layer caching.

Rustcrates.ioApache License 2.0rustdocker
2.7k143
cargo-generate/cargo-generate

cargo, make me a project

Rustcrates.ioApache License 2.0cargoliquid
cargo-generate.github.io/cargo-generate
2.5k202