Macros in Rust programming
Macros are a powerful feature in Rust programming language. Since it is compiled to syntax tree rather than string pre-processing, it has advantages over other language’s macro.
When I started with Rust macro, it was taking me to a new programming style altogether. You can easily convert the reusable part of your program to macros. It is called meta programming .
Declarative Macros
we are going to discuss only this type macro creation in this post.
Macros will try to match pattern passed , if it succeed the match proceed with code defined in curly brackets.
How to start with Rust macro:
macro_rules! macroname{
() => { }
}
macro_rules! = is the macro using in rust for creating
'macro_name' = is the name you want to be for the macro which can be invoked by *'macro_name!'*
( ) => {}
() is the sytax for pattern matching
{} is the place we need to enter the code to exceuted.
($x:expr)
$x is Name or variable
expr is type to match for name
ident
: an identifier. Examples:x
;foo
.path
: a qualified name. Example:T::SpecialA
.expr
: an expression. Examples:2 + 2
;if true { 1 } else { 2 }
;f(42)
.ty
: a type. Examples:i32
;Vec<(char, String)>
;&T
.pat
: a pattern. Examples:Some(t)
;(17, 'a')
;_
.stmt
: a single statement. Example:let x = 3
.block
: a brace-delimited sequence of statements. Example:{ log(error, "hi"); return 12; }
.item
: an item. Examples:fn foo() { }
;struct Bar;
.meta
: a “meta item”, as found in attributes. Example:cfg(target_os = "windows")
.tt
: a single token tree.
macro_rules! print_new{
//in this example we are passing the expression which has only one value which will be stored in $x
//we need to define name and designator
// here **$x** is Name or variable
// **expr** is type to match for name
($x:expr) => (
println!("value={}",$x);
)
}
fn main() {
print_new!(5);
}
In rust almost everything is expression .In the above example we passed the ‘expr’ as 5 where 5 designated as name
example 2:
In this example we formatted the pattern as x=> 10+10 which equivalent to x=>$x:expr where $x get the value of 20
macro expression passing
example3:
Here we are passing function name as Identifer to generate/define a function
macro - building function
example4:
macro -apply function to a range of numbers
example5:
macro with pass dentifiers(variables)
Reference
https://words.steveklabnik.com/an-overview-of-macros-in-rust