O'Reilly logo

Rust Essentials by Ivo Balbaert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Macros

Macros are not new to you as we have already used them. Every time we called an expression that ended with an exclamation mark (!), we called a built-in macro; the ! sign distinguishes it from a function. In our code until now, we have already used println!, assert_eq!, panic!, and vec! macros.

Why do we use macros?

Macros make powerful language or syntax extensions; therefore, they make metaprogramming possible. For example, Rust has a regex! macro that allows you to define regular expressions in your program, which are compiled while your code is compiled. This way the regular expressions are verified, they can be optimized at compile time, and there is no runtime overhead.

Macros can capture repetitive or resembling code patterns and replace ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required