17 Rust’s most popular crates

 

This chapter covers

  • Serialization and deserialization with serde
  • Time with the time module and chrono crate
  • Speeding up your code with the rayon crate
  • Errors with the anyhow and thiserror crates
  • Statics with the lazy_static and once_cell crates
  • Blanket trait implementations 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 almost think of them as extensions of the standard library—they’re not just random crates sitting around that nobody uses. Learning just these few 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 crate 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.

JSON is one of the most common ways to send requests and receive information online, and it’s is pretty simple, being made up of keys and values. Here is what it looks like:

{
   "name":"BillyTheUser",
   "id":6876
}

Here’s a longer example:

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