项目作者: mre

项目描述 :
Pssst!... see what Rust is doing behind the curtains 🕵🤫
高级语言: Rust
项目地址: git://github.com/mre/cargo-inspect.git
创建时间: 2018-11-08T21:45:54Z
项目社区:https://github.com/mre/cargo-inspect

开源协议:Other

下载


cargo-inspect

Logo

docs
Build Status

Thanks All!

ℹ️ This crate was superceded by cargo-expand, which added support for all the features that were missing
when we started to work on cargo-inspect. Thanks all for your feedback and support.

What is Rust doing behind the scenes?

There are only two ways to live your life.
One is as though nothing is a miracle. The other is as though everything is a
miracle. — Albert Einstein

Installation

You need Rust nightly and rustfmt to get started.
You can install those via rustup:

  1. rustup install nightly
  2. rustup component add rustfmt

All set? Let’s get cracking!

  1. cargo install cargo-inspect

Usage

Call it on any Rust file:

  1. cargo inspect main.rs

If you don’t specify a file, the current crate will be analyzed instead.

  1. cargo inspect

Depending on the size of the crate, this might take a while.
Please be patient.

It can also compare two file outputs! Try this:

  1. cargo inspect --diff examples/range.rs,examples/range_inclusive.rs --plain

Configuration

  1. USAGE:
  2. cargo inspect [FLAGS] [OPTIONS] [INPUT_FILE]
  3. FLAGS:
  4. -h, --help
  5. Prints help information
  6. --list-themes
  7. Should we list all pretty printer themes?
  8. --plain
  9. Don't highlight output
  10. -V, --version
  11. Prints version information
  12. -v, --verbose
  13. Print the original code as a comment above the desugared code
  14. OPTIONS:
  15. --theme <THEME>
  16. Specify a theme override for the pretty printer
  17. --diff <files>
  18. Diff input files
  19. --format <format>
  20. Override for the format that gets outputted when the `unpretty` mode is set to `flowgraph` [default: svg]
  21. --unpretty <unpretty>
  22. rustc "unpretty" parameters
  23. *Note*: For `--unpretty=flowgraph=[symbol]` you need to have `dot` on your PATH. [default: hir]
  24. ARGS:
  25. <INPUT_FILE>
  26. Input file

Background

Rust allows for a lot of syntactic sugar, that makes it a pleasure to write. It
is sometimes hard, however, to look behind the curtain and see what the compiler
is really doing with our code.

To quote @tshepang, “It is good to know what these conveniences are, to avoid
being mystified by what’s going on under the hood… the less magical thinking
we have of the world, the better.”

  • lifetime elisions
  • type inference
  • syntactic sugar
  • implicit dereferencing
  • type coercions
  • hidden code (e.g. the prelude)

I was always interested in how programming languages work in the background, how
my code was unrolled to make the compiler backend easier to maintain.

The goal is to make the compiler more approachable for mere mortals.
Mystery! Exploration! Discovery!

Read more on the background of cargo-inspect on my blog.

Code Examples

If-let gets desugared into match

Consider the following code snippet:

  1. fn main() {
  2. if let Some(x) = Some(1) {
  3. // Do something with x
  4. }
  5. }

When you compile it, the first thing Rust does is desugar it. To see what the
code looks like after this step, run

  1. cargo inspect examples/if_let.rs

This produces the following output:

Please run the command to reproduce the desugared output

You can see that the if let was desugared into a match statement.

To change the colorscheme, try cargo-inspect --list-themes, e.g.

  1. cargo inspect examples/if_let.rs --theme GitHub

Please run the command to reproduce the desugared output

Oh, and if you have graphviz installed, you can also print a pretty flowgraph from your code:

  1. cargo inspect --unpretty=flowgraph=main examples/if_let.rs

Please run the command to reproduce the desugared output

More examples

Please find more examples in the examples folder. You can also contribute
more.

The Magic Sauce

The best things in the world are assembled from simple building blocks. This
tool stands on the shoulders of giants. To work its magic, it runs the following
commands:

  1. rustc -Zinspect=hir, for retrieving the HIR.
  2. rustfmt, for formatting the output.
  3. prettyprint, for syntax-highlighting,
    which is just a wrapper around the awesome
    syntect
    and bat crates.

Contributing

This is a young project, which has downsides and upsides.

  • Everything is in flux and things can break at any time. 😫
  • There’s plenty of opportunity to shape and form the project. 😊

Thus, become a contributor today!

Known issues

As of now, this is a very fragile tool. If it fails, it might will produce
horrible output. You have been warned. That said, it won’t eat your code, of
course. :blush:

License

Licensed under either of

at your option.

Credits

Magnifying glass designed by Rawpixel.com