All moxy attributes use the #[moxy(...)] syntax. This page is a quick reference for every available attribute.
| Attribute | Description | Example |
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
| Attribute | Description | Example |
display(skip) | Exclude field from output | #[moxy(display(skip))] |
display(alias = "name") | Rename field in output | #[moxy(display(alias = "full_name"))] |
| Attribute | Description | Example |
build | Include 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))] |
| Attribute | Description | Example |
default = expr | Use 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
| Attribute | Description | Example |
get | Generate 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")))] |
| Attribute | Description | Example |
set | Generate 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()))] |
| Attribute | Description | Example |
deref | Mark field as deref target (required for multi-field structs) | #[moxy(deref)] |
| Attribute | Description | Example |
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))] |
moxy::unionize! {{
VariantName => Type,
...
} as [visibility] EnumName}
Generates an enum with tuple variants, is_*/as_*/to_* methods, and From<Type> impls.
| Attribute | Description | Example |
forward(fn sig, ...) | Declare methods to forward to this field | #[moxy(forward(fn len(&self) -> usize))] |
| Attribute | Description | Example |
forward(fn sig, ...) | Declare methods forwarded across all variants | #[moxy(forward(fn area(&self) -> f64))] |
| Attribute | Description | Example |
forward(skip) | Exclude variant from forwarding (generates unreachable!()) | #[moxy(forward(skip))] |
forward | Mark which field to forward to in named variants (defaults to first) | #[moxy(forward)] |
| Theme Name | Struct Name | Fields | Values | Punctuation |
dracula (default) | cyan | pink | yellow | white |
atom-one-dark | gold | purple | green | gray |
github-dark | blue | red | light blue | light gray |