How to do it...

Follow the given steps to implement this recipe:

  1. Create a file named sample_overloading_macros.rs, and open it in your text editor.
  2. Write the code header with the relevant information:
        //-- #########################
//-- Task: Implementing
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
  1. Create a macro named test, for which we will implement overloading:
        macro_rules! test {

($left:expr; and $right:expr) => (
println!("{:?} and {:?} is {:?}",
stringify!($left),
stringify!($right),
$left && $right)
);

($left:expr; or $right:expr) => (
println!("{:?} or {:?} is {:?}",
stringify!($left),
stringify!($right),
$left || $right)
);
}
  1. Define the main function in which we'll implement the features of the macro:
        fn main() {
test!(1i32 + 1 == 2i32; and 2i32 * 2 == 4i32);
test!(true; or false);
}

We will get the following output on the successful execution of our code:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.210.91