12 Killer Rust Libraries You Should Try

Posted on 2019-04-19

Iā€™ve just crossed a 20K LOC in one of my bigger Rust projects, and thought about pausing for a moment and sharing some great Rust libraries that Iā€™ve used.

I also plan to cover topics such as deciding between Rust vs Go, or deciding if to adopt Rust for your team or project, and what should you expect after youā€™ve decided to take it on, in a future article.

UPDATE: the second part is now out: 10 key learnings in Rust after 30,000 lines of code

clap

Building CLI tools in Rust is a match made in heavenā€Šā€”ā€Šcheck out ripgrep and Rustā€™s own Cargo. Hyperfast start up time, small binary size, type safe code, runtime safe binary, cross compiling to almost every architecture you might want.

To start building CLI tools, clap is a fantastic CLI library, itā€™s so good I donā€™t see any reason to have a dozen alternatives like common in other languages. In fact, if you want to try Rust, I would recommend taking a look at rustup and then trying out this library and see where it gets you.

serde

Like clap, serde is a feature rich and performant generic serialization library. In fact, thinking about Java and .NET, I donā€™t remember a serialization library that was this well made from all aspectsā€Šā€”ā€Šergonomics and performance.

Donā€™t try reading/writing from/to files on your own, insteadā€Šā€”ā€Šwrite your data types first and make serde do all the work. As a bonus you can mix and match the data format (YAML, JSON) after everything is done.

reqwest

Reqwest follows the gold standard for HTTP client libraries like request, superagent and requests and applies it to Rust perfectly. Itā€™s my go-to library for HTTP clients, is feature packed and complete.

rayon

Rayon is a ā€œdata parallelism library for Rustā€ or in simple words, give it data and it will know how to split it into independent chunks and work all your CPU cores.

Or even more simply, give it a list and itā€™ll parallelize map over it, amonst other features. Extremely useful for CLI tools; not all languages are good with parallelism over the command line.

slog and log

slog is a very complete logging suite for Rust. It is a core followed by many plugins such as term for terminal output, json for JSON output and more.

You should know that there is also log which aims to be part of standard Rust, and is a simpler alternative. Iā€™ve personally switched to log from slog for this reason.

itertools

Wouldnā€™t hurt to have a few more operators over your lists would it? especially as many or most of these are zero cost. With itertools you get that. Great if youā€™re a fan of libraries like lodash.

hyper

hyper is a fast HTTP implementation written in and for Rust (as opposed to those written in C, that cover for performance in dynamic languages). Youā€™ll find hyper to be in almost every high level library that you use, and if you use it directly, it feels a bit like Netty or Finagle. I found myself treating hyper both as an HTTP toolbox (using pieces from it) and as a whole, building a server over it.

Actix

Guess what doesnā€™t use hyper? Actix. Actix tries to be simpler, and from my experienceā€Šā€”ā€Šit delivers. Often I use Actix instead of Hyper because itā€™s more highlevel and for service purposesā€Šā€”ā€Šmore mature. Today, I default to using Actix directly over Hyper, unless I need to build something low level, or have a library that requires Hyper directly (thereā€™s many).

PyO3

PyO3 is one of the more popular libraries for building Rust libraries in Python (or is it Python libraries in Rust? šŸ˜€). If you want to see a result of such a mix, hereā€™s hyperjsonā€Šā€”ā€Ša JSON library for Python backed by Rustā€™s serde.

By relying on Rustā€™s safety and serdeā€™s performance, you get a safe and one of the fastest JSON libraries for Python in almost no effort.

Want to improve Python in 3 steps?

  1. Find a great Rust library
  2. Wrap it with PyO3
  3. Profit!
  • Note: PyO3 (and possibly rust-cpython) may have some design flaws that may forgo safety. Read here for more details.

proptest

proptest is a property based testing library for Rust. Ever since Iā€™ve used QuickCheck in my short Haskell stint, Iā€™ve looked for these kind of libraries in every language Iā€™ve usedā€Šā€”ā€Šlibraries that propose that theyā€™ll find a failing test case for you automatically by intelligently thinking up a set of input data that trips up your code.

libloading

For those of you who would like to mix Go or any other c-lib library into their Rust frontend, libloading make this simple.

Building medium to large projects with Rust the last year, I accept that some parts of the Rust ecosystem arenā€™t ready yet, and am not shy of building these in other languages (mostly Go)ā€” only to hook everything back into Rust with libloading.

Performance

Last thing you should know is that clean, simple Rust is very fast by default. Hereā€™s a talk about that at the correct time:

There are a couple more libraries I like to use almost always, which give you a nice performance boost out of the default that Rust already providers:

  • regexā€Šā€”ā€Ša Regex library thatā€™s really fast, including compared to other programming languages.
  • jemallocā€Šā€”ā€Šone of the fastest memory allocators, that was dropped in recent Rust versions in favor of operating system default. I found that itā€™s faster than the default OSX allocator, but use it only if you know that you need it since it adds some weight to your binaries (around 700kb).

Summary

Rust, in one word isā€Šā€”ā€ŠPower.

But thereā€™s much more to say. For exampleā€Šā€”ā€ŠRust is also extremely young compared to other programming languages.

Read more about my experience with Rust.