Rust Must-Know Crates
Christopher Durham
Posted on May 7, 2019
This is my collection of Rust crates that I often find myself using. Not every project needs to use all of them, but most projects are better off knowing that they exist. Crates are very loosely ordered within their heading from more generally useful to more niche application.
This list will be kept somewhat up to date as my preferences and the ecosystem shift. This is obviously not a complete list of great crates, but just an introduction to the great ecosystem of Rust utilities available to programmers.
Cargo Plugins
cargo-edit
This tool extends Cargo to allow you to add, remove, and upgrade dependencies by modifying your Cargo.toml file from the command line.
Currently available subcommands:
- cargo add
- cargo rm
- cargo upgrade
cargo-outdated
A cargo subcommand for displaying when Rust dependencies are out of date.
cargo-tree
A Cargo subcommand that visualizes a crate's dependency graph in a tree-like format.
cargo-update
A Cargo subcommand for checking and applying updates to installed executables.
cargo-expand
Print out the result of macro expansion and
#[derive]
expansion applied to the current crate.
cargo-modules
A cargo plugin for showing an overview of a crate's modules.
cargo-tarpaulin
Tarpaulin is designed to be a code coverage reporting tool for the Cargo build system, named for a waterproof cloth used to cover cargo on a ship. Currently, tarpaulin provides working line coverage but is still in the early development stage and therefore may contain some bugs.
cargo-audit
Audit Cargo.lock for crates with security vulnerabilities reported to the RustSec Advisory Database.
cargo-crev
A tool helping Rust users review crates they use, and share it with the community. It works as a recomendation system, helps identify poor quality, protects against many attack vectors, and aims at driving the quality of Rust ecosystem even higher, by encouraging continous peer review culture.
cargo-clone
Fetch the source code of a Rust crate.
cargo clone
will likely be added to cargo proper at some point.
Derives
derive-more
Rust has lots of builtin traits that are implemented for its basic types, such as
Add
,Not
orFrom
. However, when wrapping these types inside your own structs or enums you lose the implementations of these traits and are required to recreate them. This is especially annoying when your own structures are very simple, such as when using the commonly advised newtype pattern (e.g.MyInt(i32)
).This library tries to remove these annoyances and the corresponding boilerplate code. It does this by allowing you to derive lots of commonly used traits for both structs and enums.
Derives for:
smart-default
Custom derive for automatically implementing the
Default
trait with customized default values.
derive-builder
Rust macro to automatically implement the builder pattern for arbitrary structs. A simple
#[derive(Builder)]
will generate aFooBuilder
for your structFoo
with all setter-methods and a build method.
strum
A set of macros and traits for working with enums and strings easier in Rust.
Derives for:
Display
AsRefStr
IntoStaticStr
EnumIter
EnumCount
- and more!
Meta-programming (macros)
syn
Syn is a parsing library for parsing a stream of Rust tokens into a syntax tree of Rust source code.
proc-macro2
A runtime-compatible wrapper around the procedural macro API of the compiler's
proc_macro
crate.
proc-quote
The
quote!
macro as a procedural macro, instead of the originalquote!
macro, implemented withmacro_rules!
.The
quote!
macro turns Rust syntax tree data structures into tokens of source code.
paste
A flexible way to paste together identifiers in a macro, including using pasted identifiers to define new items.
Testing
insta
Snapshots tests (also sometimes called approval tests) are tests that assert values against a reference value (the snapshot). This is similar to how
assert_eq!
lets you compare a value against a reference value but unlike simple string assertions snapshot tests let you test against complex values and come with comprehensive tools to review changes.
trybuild
Trybuild is a test harness for invoking rustc on a set of test cases and asserting that any resulting error messages are the ones intended.
Criterion
Helps you write fast code by detecting and measuring performance improvements or regressions, even small ones, quickly and accurately. You can optimize with confidence, knowing how each change affects the performance of your code.
Other Libraries
Regex
A Rust library for parsing, compiling, and executing regular expressions. Its syntax is similar to Perl-style regular expressions, but lacks a few features like look around and backreferences. In exchange, all searches execute in linear time with respect to the size of the regular expression and search text.
lazy-static
A macro for declaring lazily evaluated statics in Rust.
Using this macro, it is possible to have statics that require code to be executed at runtime in order to be initialized. This includes anything requiring heap allocations, like vectors or hash maps, as well as anything that requires non-const function calls to be computed.
Rayon
Rayon is a data-parallelism library for Rust. It is extremely lightweight and makes it easy to convert a sequential computation into a parallel one. It also guarantees data-race freedom.
serde
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
The Serde ecosystem consists of data structures that know how to serialize and deserialize themselves along with data formats that know how to serialize and deserialize other things. Serde provides the layer by which these two groups interact with each other, allowing any supported data structure to be serialized and deserialized using any supported data format.
typetag
Serde serializable and deserializable trait objects.
This crate provides a macro for painless serialization of
&dyn Trait
trait objects and serialization + deserialization ofBox<dyn Trait>
trait objects.
Crossbeam
A set of tools for concurrent programming.
Itertools
Extra iterator adapters, functions and macros.
Rand
A Rust library for random number generation.
Rand provides utilities to generate random numbers, to convert them to useful types and distributions, and some randomness-related algorithms.
log
A Rust library providing a lightweight logging facade.
WalkDir
A cross platform Rust library for efficiently walking a directory recursively. Comes with support for following symbolic links, controlling the number of open file descriptors and efficient mechanisms for pruning the entries in the directory tree.
Directories
A tiny mid-level library that provides platform-specific standard locations of directories for config, cache and other data on Linux, Windows and macOS by leveraging the mechanisms defined by the XDG base/user directory specifications on Linux, the Known Folder API on Windows, and the Standard Directory guidelines on macOS.
tempfile
A secure, cross-platform, temporary file library for Rust. In addition to creating temporary files, this library also allows users to securely open multiple independent references to the same temporary file (useful for consumer/producer patterns and surprisingly difficult to implement securely).
num
A collection of numeric types and traits for Rust, including bigint, complex, rational, range iterators, generic integers, and more!
StructOpt
Parse command line argument by defining a struct. It combines clap with custom derive.
ScopeGuard
Rust crate for a convenient RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic).
ref-cast
Safely cast
&T
to&U
where the structU
contains a single field of typeT
.
select-rustc
Macros for conditional compilation according to rustc compiler version, analogous to
#[cfg(...)]
and#[cfg_attr(...)]
.
Posted on May 7, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.