Follow the given steps to implement this recipe:
- Create a file named sample_overloading_macros.rs, and open it in your text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: Implementing
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
- 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)
);
}
- 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: