Pssst!…看看 Rust 在窗帘后面在做什么
ℹ️ 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.
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
You need Rust nightly and rustfmt to get started.
You can install those via rustup:
rustup install nightly
rustup component add rustfmtAll set? Let's get cracking!
cargo install cargo-inspectCall it on any Rust file:
cargo inspect main.rsIf you don't specify a file, the current crate will be analyzed instead.
cargo inspectDepending 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…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."
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.
If-let gets desugared into matchConsider 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.rsThis produces the following 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 GitHubOh, and if you have graphviz installed, you can also print a pretty flowgraph from your code:
cargo inspect --unpretty=flowgraph=main examples/if_let.rsPlease find more examples in the examples folder. You can also contribute
more.
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:
rustc -Zinspect=hir, for retrieving the HIR.rustfmt, for formatting the output.prettyprint, for syntax-highlighting,
which is just a wrapper around the awesome
syntect
and bat crates.This is a young project, which has downsides and upsides.
Thus, become a contributor today!
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:
Licensed under either of
at your option.
Magnifying glass designed by Rawpixel.com
暂无开放 Issues,或尚未同步最近议题。