Misusing Macros for fn and Profit (Live @EuroRust '25!)

In this talk, Tris highlights Rust’s powerful compile-time features, particularly const functions and macros, demonstrating how they enable safer, more efficient, and expressive code by shifting logic from runtime to compile time. He encourages developers to embrace these tools responsibly to unlock Rust’s full potential, dispelling common fears about macros and showcasing their unique ability to perform complex code transformations and create domain-specific languages.

In this talk, Tris, a former Python web developer turned professional writer and Rust enthusiast, explores the powerful but often underappreciated compile-time features of Rust, focusing on const functions and macros. Tris emphasizes how Rust’s rich type system and compile-time capabilities allow developers to push logic from runtime to compile time, improving safety and efficiency. While many in the Rust community focus on type correctness and safety, Tris highlights the exciting potential of compile-time computation beyond these aspects, an area often overlooked except by Lisp programmers.

Tris introduces const functions as a subset of Rust functions that can be executed at compile time with certain restrictions, such as limited support for floating-point operations due to potential side effects. These functions allow safe, deterministic computations during compilation, enabling developers to catch errors early and optimize performance. Tris praises tools like Clippy and Bacon Clippy for helping discover opportunities to convert regular functions into const functions, thereby leveraging Rust’s compile-time capabilities more effectively.

Moving beyond const functions, Tris delves into macros, which allow arbitrary code execution at compile time and can generate or rewrite Rust code dynamically. Unlike const functions, macros have no restrictions on side effects and can perform complex code transformations, making them incredibly powerful. Tris draws parallels to Lisp’s macro system, noting that Rust’s macros enable the creation of domain-specific languages (DSLs) and sophisticated compile-time logic that many popular languages lack. This capability sets Rust apart by allowing developers to execute full Rust code during compilation.

Tris also addresses common fears and misconceptions about macros, particularly concerns about security risks from arbitrary code execution at compile time. He argues that such fears are largely unfounded, as build systems inherently run arbitrary code, and Rust simply makes this capability first-class and explicit. Tris shares a cautionary story about a workshop lathe machine to illustrate that powerful tools require careful use, not avoidance. He encourages embracing macros responsibly to unlock Rust’s full potential rather than shying away due to fear.

In conclusion, Tris celebrates Rust’s thoughtful design and the foresight of its community in providing powerful compile-time features without compromising safety or usability. He urges developers to explore and utilize const functions and macros to write more efficient, expressive, and maintainable code. By doing so, Rust programmers can solve complex problems elegantly and push the boundaries of what is possible at compile time, distinguishing Rust from many other mainstream programming languages.