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

Macros

The public macros fall into five families. The distinction is about what each macro is allowed to assume about its input, not about which procedural-macro kind happens to implement it.

Structured local syntax

These macros parse a known local shape and generate ordinary Rust:

  • callable and make_fn! give objects function-like macro syntax while retaining their original types and methods;
  • discriminated_str creates a unique string mapping and a literal-selected constructor macro;
  • enum_fn turns per-variant expressions into a method;
  • strutuct!, also exported as emmun!, generates related structs, enums, and constructor macros;
  • overload_op repeats one operator written for references across its owned operand forms and its augmented assignment;
  • complete_ops adds the operators that follow from the ones a type already has, such as subtraction from addition and negation.

Ordinary Rust fragments and names

These conveniences accept one ordinary Rust fragment or name:

Recursive whole-stream extensions

These macros rewrite arbitrary token trees recursively without assuming that the complete input is an item or expression:

Read How token rewriting behaves before combining the whole-stream extensions with attributes or macros that consume private syntax.

Composition helpers

These helpers transport or protect token streams for another macro:

  • forward_attributes moves an item-position macro invocation’s outer attributes behind a ; boundary in its opaque input;
  • excluded_macros marks macro invocations whose contents a recursive transformation must leave opaque;
  • expand! loads one out-of-line module and wraps its body in one or more whole-stream transformations before rustc parses it.

Meta-transformers

These macros transform macro and token-stream structure itself:

  • reflect! exchanges two nested invocation nodes around an opaque body;
  • perm! applies a finite permutation to comma-separated token trees while fixing the remaining positions.