Node to Rust, Day 24: Crates & Valuable Tools

Node to Rust, Day 24: Crates & Valuable Tools

Authors
Jarrod Overson

December 24, 2021

The code in this series can be found at vinodotdev/node-to-rust

Introduction

Hopefully you’ve become comfortable enough with Rust that you want to continue your journey. Rust is hard to get into but it’s worth it. I’ve found that I can build massive projects with a fraction of the unit tests I’m used to writing. Rust handles so many error cases at the compilation stage. Writing Rust is like pair programming with a grumpy old developer who catches everything.

Now you need to start building up your suite of core dependencies, the crates you rely on repeatedly and become part of your personal toolbox. These are a few of the crates in mine.

Crates

Data

  • serde - Data serialization ecosystem.
  • serde_json - JSON parsing and stringification.
  • parking_lot - Better Rust Mutex and RwLock.
  • once_cell - When you think you want global variables.

Error handling

Logging

  • log - Logging facade that abstracts logging usage away from the logger implementation.
  • env_logger - Easy console logging controlled by an environment variable.
  • test-log - Get env_logger-style output in your tests without worrying about initializing a logger.
  • pretty-env-logger - env_logger, but prettier.
  • tracing - a drop-in replacement for log and env_logger with expanded capabilities and first-class async support.

CLI

  • structopt - CLI arguments from configuration.
  • clap - CLI arguments from code.

Async/concurrency

  • tokio - Async runtime.
  • tokio-stream - Stream utilities for Tokio.
  • async-trait - For when you try to make traits with async methods.
  • crossbeam - bidirectional communication channels and more.

Web

  • rocket - An HTTP server with a great developer experience.
  • reqwest - an easy-to-use HTTP client.
  • hyper - a fast and correct HTTP implementation.

Functionality you expected in the standard library

  • rand - random number generator and related tools.
  • regex - Regular expressions.
  • base64 - Base64 implementation.
  • http - A general purpose library of common HTTP types.

Misc & tools

  • uuid - UUID implementation.
  • itertools - Additional iterator functions and macros.
  • maplit - hashmap!{} macro like vec![]
  • cfg-if - Macros to simplify mutually exclusive conditional compilation #[cfg] attributes.
  • just - a better make.

Additional reading

Wrap-up

During this series I learned that Rust has a curse. Once you learn “The Rust Way” of doing things, you get amnesia. You forget how to program any other way. This curse leads to documentation and examples that naturally skirt around problems 99% of new developers will experience in practice. I found myself unable to replicate several errors that were major roadblocks in my early Rust journey.

The Rust curse is why it’s critical that you are part of the Rust community. You are the only one that can help. You have a fresh perspective and run into real new-user problems. When you get stuck, reduce your code down to a minimally reproducible example and save it. When you eventually fix it, write about what you changed! If you don’t like writing, can’t figure something out, or are unsure about your fix, send it to me. I would be honored to take your experience and write about it.

Dont' forget to join the our Discord channel to ask questions and share with others. Keep in touch! You can reach me personally at the email above or on twitter at @jsoverson.