# Rust formatting traits - Display, Debug, hex, binary, and more

> Map Rust format syntax such as {}, {:?}, {:x}, {:b}, and {:e} to the std::fmt traits Display, Debug, LowerHex, Binary, and LowerExp.

HTML: https://rustprint.com/reference/formatting-traits/

The type suffix at the end of a Rust replacement field selects a formatting trait. `{}` uses `Display`, `{:?}` uses `Debug`, `{:x}` uses `LowerHex`, `{:b}` uses `Binary`, and the remaining suffixes map to their corresponding `std::fmt` traits.

| Syntax            | Trait      | Typical use                                    |
| ----------------- | ---------- | ---------------------------------------------- |
| `{}`              | `Display`  | user-facing text                               |
| `{:?}`            | `Debug`    | programmer-facing inspection                   |
| `{:x?}` / `{:X?}` | `Debug`    | Debug with integer fields in lower / upper hex |
| `{:b}`            | `Binary`   | base 2 integers                                |
| `{:o}`            | `Octal`    | base 8 integers                                |
| `{:x}`            | `LowerHex` | lower-case hexadecimal                         |
| `{:X}`            | `UpperHex` | upper-case hexadecimal                         |
| `{:p}`            | `Pointer`  | pointer address                                |
| `{:e}`            | `LowerExp` | lower-case exponent notation                   |
| `{:E}`            | `UpperExp` | upper-case exponent notation                   |

## Display is not automatic

Deriving `Debug` does not implement `Display`. `Display` asks the type author to choose one user-facing representation, and Rust intentionally does not provide a derive for that decision.

**A custom Display implementation**

```rust
pub struct Coordinate(pub i32, pub i32);

impl fmt::Display for Coordinate {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "({}, {})", self.0, self.1)
    }
}
```

For the complete method signature and a focused explanation of `Formatter`, `write!`, and `.to_string()`, see [implement Display for a Rust struct](/format/implement-display/index.md).

## Related references

- [Debug vs Display](/debug/debug-vs-display/index.md) for choosing between user-facing and programmer-facing output
- [Implement Display](/format/implement-display/index.md) for writing a custom `{}` representation
- [Number bases and exponent formatting](/format/number-bases/index.md) for numeric trait suffixes
- [Exact format specifier grammar](/reference/format-specifier-grammar/index.md) for the order of flags and type suffixes

Open the [interactive cheat sheet](/reference/formatting-cheat-sheet/) to compare common specifications by output.

## Sources

- [std::fmt formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)

## Related RustPrint guides

- [Implement Display for a Rust struct](/format/implement-display/index.md): Implement std::fmt::Display for a Rust struct with Formatter and write!, then use {} formatting and to_string().
- [Rust format strings - syntax, width, precision, and traits](/format/format-strings/index.md): Read Rust format strings from {} through argument capture, width, precision, alignment, escaping, and formatting traits.
- [Rust format specifier grammar](/reference/format-specifier-grammar/index.md): Read the exact order of fill, alignment, sign, alternate, zero, width, precision, and type in a Rust format specification.
