chapter seventeen

17 Rust’s most popular crates

 

This chapter covers

  • The serde crate: convert between Rust types and formats like json
  • The time module: basic functionality for time in the standard library
  • The chrono crate: work with time and time zones
  • The rayon crate: speed up your code with automatic threads for iterators
  • The anyhow and thiserror crates: easily work with errors and error types
  • The lazy static and oncecell crates: create statics that are initialized after the program starts
  • Blanket trait implementations: automatically implement your traits on other types

This chapter is sort of a cookbook of some of the most popular external crates. These crates are so common that you can think of them as sort of extensions of the standard library - they’re not just random crates sitting around that nobody uses. Learning just these few crates crates will allow you to turn data like JSON into Rust structs, work with time and time zones, handle errors with less code, speed up your code, and work with global statics.

You’ll also learn about blanket trait implementations, which are extremely fun. With those you can give your trait methods to other people’s types even if they didn’t ask for them!

17.1 serde

The serde crate is an extremely popular one that lets you convert to and from formats like JSON, YAML, and so on. In fact, it's so popular that it's rare to find a Rust programmer who has never heard of it.

17.2 Time in the standard library

17.3 chrono

17.3.1 Checking the code inside external crates

17.3.2 Back to chrono again

17.4 rayon

17.5 anyhow and thiserror

17.5.1 anyhow

17.5.2 thiserror

17.6 Blanket trait implementations

17.7 lazy_static and oncecell

17.7.1 Lazy static: lazily evaluated statics

17.7.2 OnceCell: A cell to only write to once

17.8 Summary