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

Attribute Reference

All moxy attributes use the #[moxy(...)] syntax. This page is a quick reference for every available attribute.

Display — Struct Level

AttributeDescriptionExample
display(debug)Quote string values#[moxy(display(debug))]
display(compact)Values only, space-separated#[moxy(display(compact))]
display(keyvalue)key=value pairs#[moxy(display(keyvalue))]
display(map)Map style without type name#[moxy(display(map))]
display(json)JSON serialization (requires json feature)#[moxy(display(json))]
display(pretty)Multi-line output (modifier)#[moxy(display(pretty))]
display(color)Colored output with default theme (requires color feature)#[moxy(display(color))]
display(color = "theme")Colored output with named theme#[moxy(display(color = "dracula"))]
display(alias = "name")Rename the type in output#[moxy(display(alias = "Person"))]
display("fmt", exprs...)Custom format string#[moxy(display("{}", self.name))]

Modifiers can be combined in a single attribute: #[moxy(display(debug, pretty, color))]

They can also be split across multiple #[moxy(...)] attributes — arguments are merged automatically:

#[moxy(display(debug))]
#[moxy(display(pretty))]    // equivalent to #[moxy(display(debug, pretty))]

Specifying the same option twice with different values is a compile error:

#[moxy(display(alias = "A"))]
#[moxy(display(alias = "B"))]  // error: conflicting values for `alias`

#[moxy(display(compact))]
#[moxy(display(debug))]        // error: conflicting display styles

Display — Field Level

AttributeDescriptionExample
display(skip)Exclude field from output#[moxy(display(skip))]
display(alias = "name")Rename field in output#[moxy(display(alias = "full_name"))]

Build — Field Level

AttributeDescriptionExample
buildInclude field in builder (compile error if unset at build time)#[moxy(build)]
build("name")Include field with a custom setter method name#[moxy(build("username"))]
build(default = expr)Include field with a fallback value (optional in builder)#[moxy(build(default = 8080u16))]
build("name", default = expr)Custom setter name + default value#[moxy(build("port", default = 8080u16))]

Default — Field Level

AttributeDescriptionExample
default = exprUse expression as field’s default value (passed through .into())#[moxy(default = "localhost")]

Supports literals, typed literals, constants, and arbitrary expressions:

#[moxy(default = "hello")]          // string literal
#[moxy(default = 8080u16)]          // typed literal
#[moxy(default = MAX_RETRIES)]      // constant
#[moxy(default = Vec::new())]       // expression

Get — Field Level

AttributeDescriptionExample
getGenerate getter (returns &Deref::Target — e.g. String&str. Use copy for primitives)#[moxy(get)]
get("name")Custom getter method name#[moxy(get("id"))]
get(copy)Return by value (for Copy types and primitives like u32, bool)#[moxy(get(copy))]
get(clone)Return by clone#[moxy(get(clone))]
get(mutable)Also generate field_mut(&mut self) -> &mut T#[moxy(get(mutable))]
get(on = expr)Run expression before returning#[moxy(get(on = log::debug!("read")))]

Set — Field Level

AttributeDescriptionExample
setGenerate setter (fn set_field(&mut self, value: impl Into<T>) -> &mut Self)#[moxy(set)]
set("name")Custom setter method name (replaces set_field)#[moxy(set("update_id"))]
set(on = expr)Transform: expression result is assigned (value: T in scope)#[moxy(set(on = value.to_lowercase()))]

Deref — Field Level

AttributeDescriptionExample
derefMark field as deref target (required for multi-field structs)#[moxy(deref)]

Union — Variant Level

AttributeDescriptionExample
variant(alias = "name")Rename generated methods (is_name, as_name, to_name)#[moxy(variant(alias = "ident"))]
variant(skip)Skip all method generation for this variant#[moxy(variant(skip))]

unionize! — Macro Syntax

moxy::unionize! {{
    VariantName => Type,
    ...
} as [visibility] EnumName}

Generates an enum with tuple variants, is_*/as_*/to_* methods, and From<Type> impls.

Forward — Struct Field Level

AttributeDescriptionExample
forward(fn sig, ...)Declare methods to forward to this field#[moxy(forward(fn len(&self) -> usize))]

Forward — Enum Level

AttributeDescriptionExample
forward(fn sig, ...)Declare methods forwarded across all variants#[moxy(forward(fn area(&self) -> f64))]

Forward — Enum Variant Level

AttributeDescriptionExample
forward(skip)Exclude variant from forwarding (generates unreachable!())#[moxy(forward(skip))]
forwardMark which field to forward to in named variants (defaults to first)#[moxy(forward)]

Color Themes

Theme NameStruct NameFieldsValuesPunctuation
dracula (default)cyanpinkyellowwhite
atom-one-darkgoldpurplegreengray
github-darkblueredlight bluelight gray