Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Display

The Display derive macro implements std::fmt::Display for your structs. It supports multiple output formats, custom format strings, JSON serialization, and colored terminal output.

Default Format

Without any attributes, Display produces a struct-literal style output:

use moxy::Display;

#[derive(Display)]
struct User {
    name: String,
    email: String,
}

let user = User {
    name: "John".into(),
    email: "john@example.com".into(),
};

assert_eq!(format!("{user}"), "User { name: John, email: john@example.com }");

Attribute Syntax

Display behavior is controlled through #[moxy(display(...))] attributes at the struct level and field level:

use moxy::Display;

#[derive(Display)]
#[moxy(display(debug, pretty))]      // struct-level: format + modifiers
struct User {
    name: String,
    #[moxy(display(skip))]            // field-level: skip this field
    password: String,
}

Multiple #[moxy(...)] attributes on the same item are merged, so these two forms are equivalent:

#[moxy(display(debug, pretty))]

// same as:
#[moxy(display(debug))]
#[moxy(display(pretty))]

Tip

Splitting attributes across multiple lines is useful when mixing format flags with field-level overrides — each concern can live on its own #[moxy(...)] line.

What’s Next