• Stars
    star
    384
  • Rank 111,079 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pssst!... see what Rust is doing behind the curtains πŸ•΅πŸ€«

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:

rustup install nightly
rustup component add rustfmt

All set? Let's get cracking!

cargo install cargo-inspect

Usage

Call it on any Rust file:

cargo inspect main.rs

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

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:

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

Configuration

USAGE:
    cargo inspect [FLAGS] [OPTIONS] [INPUT_FILE]

FLAGS:
    -h, --help
            Prints help information

        --list-themes
            Should we list all pretty printer themes?

        --plain
            Don't highlight output

    -V, --version
            Prints version information

    -v, --verbose
            Print the original code as a comment above the desugared code


OPTIONS:
        --theme <THEME>
            Specify a theme override for the pretty printer

        --diff <files>
            Diff input files

        --format <format>
            Override for the format that gets outputted when the `unpretty` mode is set to `flowgraph` [default: svg]

        --unpretty <unpretty>
            rustc "unpretty" parameters

            *Note*: For `--unpretty=flowgraph=[symbol]` you need to have `dot` on your PATH. [default: hir]

ARGS:
    <INPUT_FILE>
            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:

fn main() {
    if let Some(x) = Some(1) {
        // Do something with x
    }
}

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

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.

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:

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

License

Licensed under either of

at your option.

Credits

Magnifying glass designed by Rawpixel.com

More Repositories

1

idiomatic-rust

πŸ¦€ A peer-reviewed collection of articles/talks/repos which teach concise, idiomatic Rust.
4,626
star
2

the-coding-interview

Programming exercises, code katas and puzzles for your job interview training - or just for fun.
Python
1,697
star
3

hyperjson

🐍 A hyper-fast Python module for reading/writing JSON data using Rust's serde-json.
Python
503
star
4

fcat

A 3x faster implementation of cat, using splice
Rust
249
star
5

vscode-snippet

🐀 A Visual Studio Code extension for cheat.sh. Quickly and easily find code snippets for any language right inside your IDE.
TypeScript
236
star
6

kafka-influxdb

High performance Kafka consumer for InfluxDB. Supports collectd message formats.
Python
215
star
7

timelapse

🎬 Native macOS app for recording timelapse videos of your desktop.
Python
214
star
8

prettyprint

Beautifully formatted output for your terminal 🌈
Rust
203
star
9

zerocal

πŸš«πŸ“† Serverless calendar built with shuttle.rs
Rust
164
star
10

futures-batch

An adapter for futures, which chunks up elements and flushes them after a timeout β€” or when the buffer is full. (Formerly known as tokio-batch.)
Rust
67
star
11

envy

πŸ’« Manage environment variables without cluttering your .zshrc.
Rust
64
star
12

PHPench

Realtime benchmarks for PHP code
PHP
53
star
13

teams-call

Shell script to detect when you're in a Microsoft Teams Call. Supports Linux and macOS.
Python
51
star
14

mos6502

MOS 6502 emulator written in Rust
Rust
42
star
15

Creamy

A simple CMS in the style of Perch.
PHP
32
star
16

endler.dev

HTML
26
star
17

rust-for-x

Rust for {Ruby, Haskell, C, ...} programmers
24
star
18

rustly

A toy Rustlang URL shortener using Rocket
Rust
21
star
19

pandoc-memoir

Convert Markdown to beautiful PDF using pandoc, LaTeX, and chocolate donuts.
TeX
16
star
20

riffraff

A commandline interface for Jenkins (like `fly` for Concourse).
Go
15
star
21

slack

A simple, idiomatic, fully documented Rust library for the Slack Web API
Rust
11
star
22

rate-limits

Parse HTTP Rate-Limit headers of different vendors
Rust
10
star
23

svg-metadata

Read metadata information of an SVG file (e.g. viewBox size)
Rust
9
star
24

beacon

A dedicated endpoint for real user monitoring. Works with boomerang, statsc and others.
JavaScript
9
star
25

karban

Jekyll for invoices. A static invoice compiler that generates PDFs and HTML from Markdown files.
PHP
9
star
26

spaceglenda

Vectorized, colorized version of Space Glenda, the Plan 9 mascot.
8
star
27

dotvim

My VIM configuration.
Vim Script
8
star
28

pygments-rs

Rust bindings for pygments, a powerful syntax highlighter
Rust
7
star
29

cargo-deliver

πŸ“¦ Run goreleaser with cargo to publish pre-built Rust binaries on Github/Dockerhub/Artifactory
Rust
7
star
30

dropix

A self-updating, caching Dropbox gallery for your website. Customizable, too!
PHP
7
star
31

menudon

πŸ˜πŸ’¬ Mastodon client for your macOS menubar
JavaScript
6
star
32

gh-stats

Async command-line tool for retrieving the stars of all your Github projects. 🌟
Rust
6
star
33

money

Sample code for my talk Idiomatic Rust, which I gave at FOSDEM 2018.
Rust
6
star
34

docker-php-phan

A Docker image for etsy's phan, the static analyzer for PHP
Shell
6
star
35

mre.github.io.v1

Version 1 of my blog, built with Jekyll
HTML
5
star
36

teapot

πŸ«– A super simple dummy server for testing HTTP clients. No dependencies. The little brother of https://github.com/mufeedvh/binserve.
Rust
5
star
37

cargo-inspect-vscode

A Visual Studio Code extension for cargo-inspect
TypeScript
5
star
38

tracker

Realtime hand gesture recognition to control your window manager
Python
5
star
39

awesome-static-analysis-ci

Continuous integration for the awesome static analysis project
Rust
5
star
40

PHP-Crawler

Web Crawler - with email/link scraping and proxy support
PHP
5
star
41

mre.github.io.v2

Version 2 of my blog, built with Cobalt (Rust). Good times
Liquid
5
star
42

jquery.ratingbar

Create graphical ratings (stars, bars) from text: 4/5 => β˜…β˜…β˜…β˜…β˜†
JavaScript
4
star
43

lqip

A boring commandline tool for generating lqip image thumbnails
Rust
4
star
44

rust-cli-workshop

Learn Rust by writing CLI tools!
PHP
4
star
45

engldict

Dabblings into language learning
Python
4
star
46

edgecast

A golang client for the Edgecast API
Go
3
star
47

mos6532

MOS 6532 Emulator
Rust
3
star
48

cudampi

Large hybrid CPU/GPU sorting network using CUDA and MPI
C++
3
star
49

kafka-benchmark

A simple kafka script for benchmarking kafka message throughput
Python
3
star
50

freq

πŸ—Ό A CLI term frequency analyzer. Counts the number of occurrences of each word in an input and creates formatted output or a histogram.
Rust
3
star
51

Process

A lightweight PHP wrapper for interactive subprocesses
PHP
3
star
52

rust-language-bindings

A list of available language bindings for Rust
2
star
53

mre

This is m(r)e
2
star
54

past

Rust
2
star
55

async-talk

Slides and additional material for my talk about asynchronous programming
HTML
2
star
56

swarm-demo

Demonstrates the networking and load-balancing power of Docker Swarm
Go
2
star
57

jbmc

Java Bounded Model Checker
Java
2
star
58

envsync

Keeps `.env` files in sync with `env.sample`.
Rust
2
star
59

jQuery.anchorfy

Create a linked list of all headers inside a container. Supports animations and is easy to adjust.
HTML
2
star
60

bromance

A friendly commandline tool for bro and tldr
Python
1
star
61

maxminddb-rust-bench

Rust
1
star
62

mre.github.io.v3

Version 3 of my blog, built with Zola (Rust). It was a great time.
HTML
1
star
63

stream-processors

Just a list of stream-processors
1
star
64

GnuPlot

A thin wrapper around GnuPlot
PHP
1
star
65

npm-quickCal

an easy to use booking appointment calendar
JavaScript
1
star
66

DrawRoom

Distraction free drawing
Python
1
star
67

peek

Experiments with an alternative remote desktop implementation
Python
1
star
68

voctokey

Control voctomix remotely via keyboard shortcuts
Python
1
star
69

accs

Parse menu of the ACCS cantine DΓΌsseldorf.
Python
1
star
70

llama

A silly game written in Python using cocos2d
Python
1
star
71

toypc

A shitty Rust implementation of http://adventofcode.com/day/23
Rust
1
star
72

ideas

Pretty please make them real.
1
star
73

sheldon

A wonderful directory of useful shell-scripts
Shell
1
star
74

collectd

A docker image for collectd compiled from source
1
star
75

Talks

...mostly Computer Science related.
JavaScript
1
star
76

live

Live programming / workshop material
HTML
1
star