All Projects → dtolnay → Cargo Expand

dtolnay / Cargo Expand

Licence: other
Subcommand to show result of macro expansion

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to Cargo Expand

cargo-wix
A cargo subcommand to build Windows installers for rust projects using the WiX Toolset
Stars: ✭ 142 (-83.97%)
Mutual labels:  cargo
Jql
A JSON Query Language CLI tool
Stars: ✭ 368 (-58.47%)
Mutual labels:  cargo
Mongo Rust Driver
The official MongoDB Rust Driver
Stars: ✭ 633 (-28.56%)
Mutual labels:  cargo
Wargo
Easy Rust to WebAssembly
Stars: ✭ 260 (-70.65%)
Mutual labels:  cargo
Cargo Profiler
Cargo subcommand to profile binaries
Stars: ✭ 348 (-60.72%)
Mutual labels:  cargo
Tokamak
Fusion Reactor for Rust - Atom Rust IDE
Stars: ✭ 404 (-54.4%)
Mutual labels:  cargo
broom
A disk cleaning utility for developers.
Stars: ✭ 38 (-95.71%)
Mutual labels:  cargo
Cargo Fuzz
Command line helpers for fuzzing
Stars: ✭ 725 (-18.17%)
Mutual labels:  cargo
Juniper
GraphQL server library for Rust
Stars: ✭ 4,187 (+372.57%)
Mutual labels:  cargo
Cargo Release
Cargo subcommand "release": everything about releasing a rust crate.
Stars: ✭ 601 (-32.17%)
Mutual labels:  cargo
Cargo
📦 GitHub Action for Rust `cargo` command
Stars: ✭ 267 (-69.86%)
Mutual labels:  cargo
Cargo Lipo
Cargo subcommand to automatically create universal libraries for iOS.
Stars: ✭ 290 (-67.27%)
Mutual labels:  cargo
Cargo Deny
❌ Cargo plugin for linting your dependencies 🦀
Stars: ✭ 533 (-39.84%)
Mutual labels:  cargo
blackjack
Build cargo dependencies with Bazel
Stars: ✭ 34 (-96.16%)
Mutual labels:  cargo
Cargo Generate
cargo, make me a project
Stars: ✭ 686 (-22.57%)
Mutual labels:  cargo
rustsec
Audit Cargo.lock files for dependencies with security vulnerabilities
Stars: ✭ 956 (+7.9%)
Mutual labels:  cargo
Kondo
Save disk space by cleaning non-essential files from software projects.
Stars: ✭ 373 (-57.9%)
Mutual labels:  cargo
Cargo Audit
Audit Cargo.lock files for crates with security vulnerabilities
Stars: ✭ 735 (-17.04%)
Mutual labels:  cargo
Rust Python Example
Example of using Rust to Extend Python
Stars: ✭ 699 (-21.11%)
Mutual labels:  cargo
Rustfix
Automatically apply the suggestions made by rustc
Stars: ✭ 586 (-33.86%)
Mutual labels:  cargo

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 -- -Zunstable-options --pretty=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.

Cargo expand relies on unstable compiler flags so it requires a nightly toolchain to be installed, though does not require nightly to be the default toolchain or the one with which cargo expand itself is executed. If the default toolchain is one other than nightly, running cargo expand will find and use nightly anyway.

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 if there is one (usually ~/.cargo/config).

Set the default syntax highlighting theme with the theme setting:

[expand]
theme = "TwoDark"

Run cargo expand --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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].