liveBook · Manning

Compile time

As explained in Write Powerful Rust Macros

Compile time is the phase in which source code is analyzed and transformed into an executable or library artifact.

Compile time is the phase when source code is analyzed and transformed into an executable or library artifact. Rust macros run here, expanding into ordinary structs, functions, and related code before linting, type checking with cargo check, library compilation, or binary generation by rustc. section 1.2

Treat compile-time generation as a practical trade-off:

  • Use macros to reduce boilerplate or duplication only when the resulting code remains understandable. section 1.3.2
  • Prefer structs, enums, functions, generic functions, or blanket implementations when they express the solution more clearly. section 1.3.2
  • Account for increased compile times and binary size; small macros may impose negligible cost, while production-grade macros can create a significant trade-off. section 1.3.2

Generated code hidden behind a simple example

A concise Rust program can conceal many lines generated by macros. After expansion, those lines become ordinary code that proceeds through linting, type checking, and compilation. section 1.2

Figure 1.1 I contain multitudes! How our simple example hides many more lines of code, generated by macros.
figure

Visualizes how a small example can contain substantially more macro-generated code before later compilation stages. section 1.2

How Write Powerful Rust Macros covers it

How deep it goes
Traces macro expansion through later build stages but does not detail compiler internals or expansion implementation.
Examples
Uses macro-generated structs and functions, a hidden-code figure, and a blanket implementation for Copy types.
What you can do after
Evaluate compile-time cost and choose macros only when their boilerplate reduction outweighs understandability and build-time trade-offs.
How it connects
Connects compile time with metaprogramming, macros, linting, type checking, generic functions, and blanket implementations.

Also explained in

Compare how 2 books explain compile time →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.