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

Enums

The Display derive works with enums. Each variant is rendered independently based on its kind (unit, tuple, named).

Default Style

use moxy::Display;

#[derive(Display)]
enum Shape {
    Circle(f64),
    Rect { width: f64, height: f64 },
    Point,
}

assert_eq!(Shape::Circle(3.14).to_string(), "Circle(3.14)");
assert_eq!(Shape::Rect { width: 4.0, height: 2.0 }.to_string(), "Rect { width: 4, height: 2 }");
assert_eq!(Shape::Point.to_string(), "Point");

Enum-Level Style

Apply a style to all variants at once:

use moxy::Display;

#[derive(Display)]
#[moxy(display(debug))]
enum Value {
    Text(String),
    Num(i32),
}

assert_eq!(Value::Text("hi".into()).to_string(), "Text(\"hi\")");
assert_eq!(Value::Num(42).to_string(), "Num(42)");

Per-Variant Overrides

Variant-level #[moxy(display(...))] overrides the enum-level style:

use moxy::Display;

#[derive(Display)]
#[moxy(display(debug))]
enum Token {
    #[moxy(display(compact))]
    Ident(String),
    Literal { value: String, kind: String },
}

// Ident uses compact (override), Literal uses debug (enum default)

Custom Format Strings

use moxy::Display;

#[derive(Display)]
enum Expr {
    #[moxy(display("{x}x{y}"))]
    Rect { x: i32, y: i32 },
    Point,
}

assert_eq!(Expr::Rect { x: 4, y: 2 }.to_string(), "4x2");
assert_eq!(Expr::Point.to_string(), "Point");

Field Attributes

skip and alias work within named variants:

use moxy::Display;

#[derive(Display)]
enum Entry {
    User {
        name: String,
        #[moxy(display(skip))]
        password: String,
    },
}

All struct-level styles work per-variant: debug, compact, keyvalue, map, json, color, pretty, alias.