liveBook · Manning

Display

As explained in Rust in Action: Systems programming concepts and techniques

The Display trait is Rust's standard formatting trait for producing a user-facing textual representation of a value.

Display is Rust’s standard trait for turning a value into user-facing text. Macros such as println!, print!, write!, writeln!, and format! use Display implementations when formatting with {}. section 3.6.2

A type opts into Display with impl Display for Type and must provide fmt, which receives a mutable Formatter and returns fmt::Result. Unlike an automatically derived Debug representation, Display lets you choose the text. section 3.6.2

Formatting a file value

Implement Display for FileState by matching Open and Closed, then implement Display for File with write!(f, "<{} ({})>", self.name, self.state). Because FileState also implements Display, the File implementation can format its state with {}. section 3.6.2

Displaying a wrapper error

An error enum containing I/O and address-parsing errors can implement Display with write!(f, "{:?}", self), reusing its Debug representation before implementing std::error::Error. section 8.5.2

A Display implementation typically follows these steps:

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines the fmt contract and demonstrates nested implementations, but leaves Formatter details largely unexplored.
Examples
Uses File and FileState formatting plus an UpstreamError enum that reuses Debug output.
What it assumes
Assumes basic Rust structs, enums, traits, and formatting syntax while introducing custom trait implementations through concrete code.
What you can do after
Build it from scratch by implementing Display for structs, enums, nested fields, and wrapper error types.
How it connects
Connects Display with Debug, formatting macros, traits, Error, enums, map_err, and From.

Also explained in

Compare how 3 books explain Display →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.