Cargo is a fantastic tool. It’s number 1 or 2 on my list whenever someone asks me to describe the benefits of Rust.

This post is about little crates that you can install to extend cargo’s day-to-day functionality. They add things that may be a little opinionated, making the core developers leave them out of the main tool and instead available as community maintained addons.

First I’ll mention 3 great tips for cargo itself:

Tips for cargo itself

cargo doc

The secret weapon of the Rust community.

Make sure you know how to document your crates (tldr /// comments annotate ) and the cool features you can do like running tests in the docs. Then you can see these docs with cargo doc!

A great thing I’ve found is that you can use cargo doc --open to directly open the browser to the generated docs. Before that I was manually looking for the docs under target/doc/.

cargo check

Even though it’s well known, I often forget to use this! cargo check! It doesn’t compile & link your code into an executable, it simply checks that your code is valid so it’s much faster.

cargo test

When using a language it is KEY to know how to run specific tests. As a recap with cargo this is cargo test $yourtestname, which is also a substring match, so it will run all tests with yourtestname in their name, like yourtestname2.

Ok, now onto what you’re here for, cargo extensions!

Useful: I use these nearly every day

cargo-edit

Lets you type cargo add mycrate to add mycrate as a dependency to your project.

IIRC the cargo developers didn’t want to add this functionality in by default with the tool, as a sort of nudge to use fewer dependencies, but it’s just a nice feature to have so you don’t have to go digging for the latest version number yourself.

cargo-watch

Watches your rust project and re-runs cargo commands when it changes. Mostly I use it to be able to have an IDE-like experience:

cargo watch -x check
cargo watch -x run --example example_im_tweaking

You can watch non Rust files and execute more complex commands, but that’s what I use it for most often.

I use these occasionally

cargo-instruments

Helpful extension for profiling your Rust application on macOS. This collects performance information and simplifies the process of running Instruments with Rust code.

cargo-flamegraph

Same deal, but with perf on linux. Collects a nice flamegraph for your Rust application.

cargo-tree

Shows your dependency tree in a clear way. Very useful with the -d option, which inverts the tree to show you when and why you have the same crate installed twice.

cargo-outdated

Shows you which dependencies you can bump with semver.

Crates I’ve never used, but I’m curious about

cargo-audit

Shows you which of your dependencies have known vulnerabilities in them.

cargo-readme

Builds your readme out of rustdoc so the examples are never out of date! Sounds neat.

cargo-benchcmp

Lets you compare the output of two cargo bench runs.

cargo fuzz and cargo-afl

Lets you use libfuzzer or AFL to fuzz your program. I’ve used afl before and it was really fun, but I haven’t been in the position of needing to use one for my Rust work yet.

cargo-crev

Cargo-crev is attempting to build a web of trust system for auditing crates. I’m not big on Web Of Trust but sounds interesting.

Conclusion

So there you have it, some little niceties for writing lots of Rust.


comments powered by Disqus