Procedural Macros
Learn about procedural macros, focusing on the different types of procedural macros. Finish up by learning how we can create a procedural macro of our own.
We'll cover the following
Procedural macros in Rust are a very powerful tool. They act as syntax extensions, allowing us to add additional functionality to Rust by writing code in the form of functions. We'll be looking at a few types of procedural macros in this lesson, so let’s delve right in.
Derive macros
Derive macros, also known as #[derive]
macros, automatically implement common traits for custom data types. By annotating a struct or an enum with the #[derive]
attribute, you can use procedural macros to generate implementations for traits such as Clone
, Debug
, and Serialize
. Here’s an example:
Get hands-on with 1400+ tech skills courses.