Libraries
There are many excellent Rust libraries
(and also many poor ones of course).
These are all collected at crates.io
,
the Rust language-specific package repository.
For most programs, use of ecosystem library packages is a practical necessity.
Rust's excellent metaprogramming system makes it possible for libraries to provide facilities that resemble bespoke language features.
When searching for libraries,
usually use the opinionated catalogue at lib.rs
.
Or use "recent downloads" for the search order on crates.io
,
which is inexact but is likely to give you fate-sharing
with the rest of the community, at least.
Libraries you should know about
-
itertools
. Superb collection of extra iterator combinators. -
num
,num-traits
,num-derive
. Not just for "numeric" code - helpful integer conversions etc. too. -
strum
. Iterate over enum variants; enums to strings, etc. -
slab
,generational_arena
orslotmap
. Heap storage tools which safely sidestep borrowck (and are fast). -
index_vec
.arrayvec
.indexmap
. Variations onVec
andHashMap
. -
easy-ext
. Conveniently define methods on other people's types. -
rayon
: Semi-magical safe multicore parallelism as a drop-in replacement for std's serial iterators. -
parking_lot
. Alternatives to the standard mutex etc.parking_lot::Mutex
is const-initialisable;std
's only in Rust 1.63 (August 2022). -
crossbeam
: other tools for multithreaded programming, including scoped threads. -
chrono
(ortime
) for human-readable date/time handling API is a bit funky. Be sure to usechrono-tz
on Unix. -
lazy_static
,once_cell
for data to be initialised once.
Libraries for specific purposes
-
log
(andenv_logger
, etc.);tracing
. -
ndarray
,ndarray-linalg
, etc. Vectors, matrices, linear algebra. -
Cryptography:
ring
,rustls
, Rust Crypto; see Sylvain Kerkour's 2021 writeup. -
bstr
: Stringish methods on byte strings that are hopefully UTF-8 (but might not be). -
bytemuck
: Reinterpret-casting of plain data.
serde
serde is a serialisation/deserialisation framework.
It defines a data model,
and provides automatic translation of ordinary Rust struct
s
to and from that model.
Ecosystem libraries provide concrete implementations for a wide variety of data formats, and some interesting data format metaprogramming tools.
The result is a superb capability to handle a wide variety of data marshalling problems. serde is especially good for ad-hoc data structures and structures whose definition is owned by a Rust project.
serde and its ecosystem are considerably better for many tasks than anything available in any other programming environment.
Generally, the resulting code is a fully monomorphised open-coded marshaller specialised for the specific data structure(s) and format(s), so performance is good but the code size can be very large.
Web tools and frameworks
Most Rust web tools are async.
Use reqwest
or ureq
for making HTTP requests.
Use hyper
for a raw HTTP client or server,
but consider using reqwest
(client)
or a web framework (server) instead.
Rust is well supplied with web frameworks, but it is hard to choose.
-
I have been using Rocket for some years, But development is rather slow - there still isn't a final release of
rocket 0.5
. You should use the 0.5 preview. -
axum
is from the Tokio team, but quite new. -
actix-web
is very popular, but sometimes lacking attention to detail. -
rouille
is sync. Yay! But I haven't tried it. -
You should perhaps also consider:
warp
.
Command line parsing: clap
If you are writing a command line program
you should probably use clap
.
It allows declarative definition of command line options.
Unfortunately,
clap
has some problems.
-
Serious problems handling options which override each other. There is a facility for this but it is not convenient and its algorithm is fundamentally wrong.
-
General failure to follow (at least by default) well-established Unix option parsing conventions.
To illustrate:
it is quite awkward even to provide a conventional pair of
mutually-overriding --foo
and --no-foo
options.
In practice, using clap
means accepting that one's program will have
an imperfect and sometimes-balky command line syntax.
There are alternatives,
notably getopts
, gumdrop
and argparse
,
but they are much less popular and less well maintained.
I sometimes use argparse
where I want a fine-tuned option parser,
but it is quite odd and the docs are not great.