Rust Resources

Written
  • 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, and futures 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 like AsyncRead 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
      • fxhash is a good default hash replacement if you DO NOT need a cryptographically secure hash. If you're running a service that stores user-defined input in a hash table then don't use it.
      • 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
      • 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
      • 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
        • #[derive(Error, Debug)]
          pub enum RequestError {
            #[error("Invalid query string: {0}")]
            QueryStringError(#[from] serde_qs::Error),
          
            #[error(transparent)]
            Other(#[from] anyhow::Error),
          }
    • Argument Parsing
      • structopt lets you define command-line arguments declaratively
        • #[derive(Debug, StructOpt)]
          #[structopt(name="myapp")]
          struct Args {
            // Can add `default` and a bunch of other things as well
            #[structopt(long="file", short="f", help="")]
            file: PathBuf,
            
            // Can use long and short without values to get the "obvious" defaults
            /// Help text can go in a doc comment like this too
            #[structopt(long, short)]
            tag: Option<String>,
            
            // default_value needs to be a string, even for number values
            #[structopt(long="count", short="c", default_value="5")]
            count: u32,
            
            // array of arguments
            #[structopt(long, short)]
            values: Vec<String>,
          }
          
      • structopt-toml works with structopt to load from a TOML file and let the command line override the values.
        • derive StructOptToml on the Args struct as well to use this
        • Then use Args::from_args_with_toml(toml_str) to parse
    • 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.
        • 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. Or query_as! to use an existing type.
      • diesel for a full-fledged ORM if that's your style. Only supports Postgres but you should probably be using Postgres anyway :)
    • Image Handling
      • image for general image loading and running filters
      • pix is another option. Haven't tried it though.
        • It works with footile to do plotter-style image manipulation
      • ab_glyph and glyph_brush_layout for text rendering
    • Miscellaneous
      • nom for building parsers
      • katex for rendering LaTeX
      • syntect for syntax highlighting
        • I've heard that tree-sitter is good for this too but haven't tried it.
      • handlebars is one of many templating options
      • delegate for delegating method calls to the same method on an internal field.
  • Learning
  • Debugging

Thanks for reading! If you have any questions or comments, please send me a note on Twitter. And if you enjoyed this, I also have a newsletter where I write about tech thoughts, interesting things I've read, and project updates each Thursday.

You can check out a recent issue, or enter your email below to subscribe.