Rust Macros and Functions

 Rust Macros and Functions

Rust Macros and Functions


Introduction

The term macro refers to a family of features in Rust: declarative macros with macro_rules! and three sorts of procedural macros:

  • Custom #[derive] macros that specify code added with the derive attribute used on structs and enums
  • Attribute-like macros that outline custom attributes usable on any item
  • Function-like macros that appear as if the function calls but operate the tokens specified as their argument

The Difference between Macros and Functions

Fundamentally, macros are how of writing code that writes other code, which is understood as meta-programming. Derive attribute, which generates an implementation of varied traits for us. All of those macros expand to supply more code than the code we’ve written manually.

Meta-programming is beneficial for reducing the quantity of code we've to write down and maintain, which is additionally one among the roles of functions. However, macros have some additional powers that functions don’t.

A function signature must declare the amount and sort of parameters the function has. Macros, on the opposite hand, can take a variable number of parameters: we will call println!("hello") with one argument or println!("hello {}", name) with two arguments. Also, macros are expanded before the compiler interprets the meaning of the code, so a macro can; for instance, implement a trait on a given type. A function can’t because it gets called at runtime and a trait must be implemented at compile time.

The downside to implementing a macro rather than a function is that macro definitions are more complex than function definitions because we’re writing Rust code that writes Rust code. Thanks to this indirection, macro definitions are generally harder to read, understand, and maintain than function definitions.

Another important difference between macros and functions is that we simply must define macros or bring them into scope before we call them during a file, as against functions we will define anywhere and call anywhere.

Declarative Macros with macro_rules! For General Meta-programming

The most widely used sort of macros in Rust is declarative macros. These also are sometimes mentioned as "macros by example," "macro_rules! Macros or simply plain "macros." At their core, declarative macros allow us to write down something almost like a Rust match expression. Match expressions are control structures that take an expression, compare the resulting value of the expression to patterns, and then run the code related to the matching pattern. Macros also compare a worth to patterns that are related to a particular code: during this situation, the worth is that the literal Rust ASCII text file passed to the macro; the patterns are compared with the structure of that source code; and therefore the code related to each pattern, when matched, replaces the code passed to the macro. This all happens during the compilation

Powered by Blogger.