Rust Resources
Written
— Updated
- Awesome Rust
- Rust Design Patterns
- https://lib.rs/ is great for browsing for crates for a specific purpose or just for fun
https://docs.rs/CRATE_NAME
for docs for any public crate.- Crates
- Asynchronous Code
tokio
,async_std
, andfutures
are the base crates. Tokio and Async_std are sort of "competing" and there's still a lot of similar-but-slightly-different versions of core traits likeAsyncRead
between them.- tokio-util has a lot of nice adaptors and things that makes it easier to work with streams, readers, writers, etc.
- Performance and Data Processing
- rayon for super-easy parallelism
- itertools adds a bunch of useful functions that work with
Iterator
- ahash is a good default hash replacement.
- It is not cryptographically secure. But unlike many non-cryptographically secure hashes, it is designed to be DOS resistant, so it's OK to use it for HashMaps that take user-provided input.
- ahash also uses a random seed initialization and is not guaranteed to have stable output across versions or platforms, so you generally should not use it in situations where a hash will be stored to disk or sent across the network, but it is great for in-memory data structures.
- smallvec when you are making a lot of mostly-small vectors and most, but not all of them, will be below a certain size.
- arrayvec when you are making a lot of small vectors and know that they will never exceed a certain size
- aho-corasick for performing multiple substring searches at once in a larger string
- Error Handling
- error-stack makes it easy to retain the entire stack of errors throughout execution, ensuring nothing is lost. It also allows attaching additional context to any error in the stack.
- This isn't a replacement for
thiserror
, but a great complement to it, and I use it in all my projects now.
- This isn't a replacement for
- anyhow is great for just merging random errors together when you don't really care otherwise and just want to get the error message out and all the various errors to have the same type. (i.e. most CLI utilities)
- eyre is a fork of
anyhow
with emphasis on better error reporting- color-eyre works with
eyre
for much nicer error reporting
- color-eyre works with
- thiserror is nice for structured error types where you want to add extra formatting or otherwise handle errors in a more detailed way.
- Can wrap an anyhow inside a thiserror as well
#pub enum RequestError
- error-stack makes it easy to retain the entire stack of errors throughout execution, ensuring nothing is lost. It also allows attaching additional context to any error in the stack.
- Argument Parsing
- clap lets you define command-line arguments declaratively, with options and subcommands.
- clap lets you define command-line arguments declaratively, with options and subcommands.
- CLI
rustyline
for readline-like behaviordialoguer
for prompts- inquire is another prompt library
indicatif
for progress indicatorsclap
can also be used to create a REPL. See https://github.com/dimfeld/perceive/blob/master/crates/perceive-cli/repl.rs for an example.- ratatui for complex terminal-based UI
- Database
sqlx
for connecting to a database and running queries.- Contains connection pools, migration support and all that.
- You can derive
sqlx::FromRow
on structs to read query results into a structure. - Derive
sqlx::Type
on enums to translate them to strings (or Postgres types) - Supports a bunch of different async runtimes and all the popular SQL databases.
- Supports compile-time query validation and result type generation with the
query!
macro. Orquery_as!
to use an existing type. - sqlx also provides a
QueryBuilder
structure for helping with dynamic query generation including dynamic bindings
diesel
for a full-fledged ORM if that's your style. I've been using this recently and it's nice, with the usual caveats around having to fall back to raw SQL to do more complex queries. (See Diesel Rust ORM)sea_orm
is another up-and-coming ORM, built on async and with more attempt to provide database-agnostic behavior at runtime.
- Image Handling
image
for general image loading and running filterspix
is another option. Haven't tried it though.- It works with
footile
to do plotter-style image manipulation
- It works with
ab_glyph
andglyph_brush_layout
for text renderingresvg
for rendering SVG, andusvg
for parsing SVG sourcetiny-skia
for drawing.resvg
uses this internally
- Miscellaneous
- nom for building parsers
- katex for rendering LaTeX
- aarc-rs/aarc: Atomically updatable variants of Arc and Weak for lock-free concurrency.
- syntect for syntax highlighting
- I've heard that tree-sitter is good for this too but haven't tried it.
- handlebars, liquid, and tera are popular libraries for text templates. I'm currently mostly using
tera
since it supports template inheritance but they're all good. - opendal and object_store for abstracting IO operations over different storage such as local disk and various cloud storage services.
- delegate for delegating method calls to the same method on an internal field.
- ouroboros and self_cell for allowing members of a structure to reference other owned members in the structure, with some additional methods to build and borrow the data.
- serde_with has a bunch of macros that can customize how data is serialized and deserialized
- tempfile for managing temporary files that will automatically clean themselves up
- strum includes a bunch of functionality for enums such as iterating over the members and autogenerating a
Display
impl. - Arena Allocators
- bumpalo skips calling
Drop
but allows mixed types of allocations in an arena. - typed-arena calls
Drop
but is limited to allocating objects of one type per arena.
- bumpalo skips calling
- validator is a derive macro for easily adding validation logic to a struct.
- typed-builder creates "builder" functionality for a struct
- pretty_assertions provides replacement
assert_eq
and similar macros that print a diff of the output on both sides on a mismatch
- Asynchronous Code
- Learning
- The Official Book
- Learning Rust with too many Linked Lists
- This gets into more advanced usage than you'll usually need, but this is what really helped the ownership model click for me. I read it a few years ago so it's quite possible that there are better options now.
- https://github.com/rust-lang/rustlings
- Exercises for learning Rust
- Debugging
- Use the CodeLLDB extension in VS Code
- For vim, https://alpha2phi.medium.com/setting-up-neovim-for-rust-debugging-termdebug-and-vimspector-df749e1ba47c walks through the process of setting up CodeLLDB with the Vimspector plugin.
- In unit tests use
cargo test -- --nocapture
to show anything the tests write to stdout