liveBook · Manning

Chapter 17: Rust’s most popular crates

What you will learn in chapter 17 of Learn Rust in a Month of Lunches

The chapter introduces widely used external crates as practical additions to Rust and briefly states what blanket trait implementations make possible.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces widely used external crates as practical additions to Rust and briefly states what blanket trait implementations make possible.

Key terms: external crates, standard library, serde, chrono, rayon, anyhow, blanket trait implementations, trait methods

17.1 serde

This section explains how serde converts between Rust types and data formats such as JSON, using a struct and a JSON request as its main example.

Key terms: serde, JSON, Rust type, Serialize, Deserialize, NewUserRequest, serde_json, from_str(), serde_yaml, serde data model

17.2 Time in the standard library

The section explains how Rust’s standard library measures elapsed time, represents system time, and pauses threads, while showing its limits for working with dates.

Key terms: Instant, Instant::now(), Duration, .elapsed(), SystemTime, UNIX_EPOCH, monotonic, std::time, chrono, human-readable date

17.3 chrono

The section explains why time-related types can be complex, then introduces chrono’s timezone-free Naive types and basic ways to create them.

Key terms: time zones, calendars, chrono, Naive, NaiveDate, NaiveDateTime, time zone, from_, _opt, Option<NaiveDate>

17.3.1 Checking the code inside external crates

This section uses chrono’s date methods to show why checking an external crate’s source can reveal possible panics.

Key terms: chrono, from_ymd(), from_ymd_opt(), _opt, Option, .expect(), source, external crates, panics

17.3.2 Back to chrono

This section illustrates how to convert between standard-library time values and chrono date-time types, then applies a UTC-to-local-time conversion to timestamped events.

Key terms: SystemTime, UNIX_EPOCH, NaiveDateTime, DateTime<Utc>, FixedOffset, DateTime<FixedOffset>, UTC timestamp, Korea/Japan time zone, KoreaJapanUserEvent

17.4 Rayon

Rayon makes familiar iterator work parallel, but its speed benefit depends on the machine, the workload, and compiler optimization.

Key terms: Rayon, .par_iter(), parallel, thread::spawn(), available_parallelism(), Instant, .elapsed(), microseconds, Release, optimization

17.5 Anyhow and thiserror

The section explains why a Rust developer might move from temporary panics to Anyhow or thiserror for error handling.

Key terms: Anyhow, thiserror, .unwrap(), .expect(), Box<dyn Error>

17.5.1 Anyhow

This section shows how Anyhow handles several possible error types in one return type, how to add context to an error, and when a separately defined error type is needed.

Key terms: Anyhow, anyhow::Error, std::io::Error, question mark operator, anyhow!, .with_context(), Display, std::error::Error, derive(Error), thiserror

17.5.2 thiserror

This section shows how thiserror helps define a library error enum with displayed messages and conversions from other error types.

Key terms: thiserror::Error, #[derive(Error)], #[error], std::error::Error, Display, Debug, #[from], From, error enum

17.6 Blanket trait implementations

The section explains how blanket trait implementations give methods or conversions to groups of types, then shows how to recognize and use examples in custom code and the standard library.

Key terms: blanket trait implementation, generic type T, trait bound, std::error::Error, SystemError, ExternalCrateError, .to_system_error()?, Display, ToString, .to_string()

17.7 lazy_static and once_cell

This section explains why lazy_static and once_cell remain useful for static variables that cannot be initialized with const functions.

Key terms: lazy_static, once_cell, const fn, lazily initiated statics

17.7.1 Lazy static: Lazily evaluated statics

This section shows how lazy_static lets a program define a globally accessible static whose initialization requires runtime code.

Key terms: static, const, lazy_static, runtime, lazy_static!, static ref

17.7.2 OnceCell: A cell to only write to once

This section shows how OnceCell holds a value initialized once, including after the program has started, and identifies its thread-safe and non-thread-safe forms.

Key terms: OnceCell, OnceCell::new(), .set(), Logger, lazy_static, sync, unsync, std::cell::OnceCell, std::sync::OnceLock

17.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 16: Const, “unsafe” Rust, and external crates Chapter 18: Rust on your computer →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.