12 Killer Rust Libraries You Should Try
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?
- Find a great Rust library
- Wrap it with PyO3
- 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.