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.