← All notes

fmt::Display: how a Rust type prints with {}

Answer: The fmt module provides runtime support for the format! family of macros. To use the {} marker, implement the Display trait. The format string must be a string literal so the compiler can check it.

format! and Display

Runtime support for the format! syntax extension lives in this module.

The macro writes formatted arguments into strings at runtime.

Calling to_string uses the Display formatting trait.

The format string has to be a string literal. A variable is not accepted, so the compiler can check the string.

Implementing the trait

Debug output is often not compact or clean, so customizing the appearance is useful.

Implement fmt::Display yourself when you want the {} print marker to look the way you need.

The trait's fmt method must use a specific signature.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Rust Standard Library, std::fmt; Rust By Example, Display. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Rust Standard Library, std::fmt; Rust By Example, Display. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:he8eqh6f

iinventedlight. 2026. "fmt::Display: how a Rust type prints with {}." iinventedlight Library, iil:note:he8eqh6f, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-display/. License: CC-BY-4.0.

@misc{iil_note_he8eqh6f,
  author = {{iinventedlight}},
  title = {{fmt::Display: how a Rust type prints with \{\}}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/rust-display/},
  note = {Permanent ID iil:note:he8eqh6f. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-display.md.