Let's go ahead and make our macro a bit more complex by adding more rules in our macro, the rules are basically pattern matching cases. In this recipe, the key takes-away will be to learn how we can define pattern matching cases in macro rules.
We will require the Rust compiler and any text editor to develop the Rust code snippet.
Follow the mentioned steps to implement this recipe:
- Create a file named
sample_match.rs
, and open it in your text editor. - Write the code header with the relevant information:
//-- ######################### //-- Task: Implement matching //-- Author: Vigneshwer.D //-- Version: 1.0.0 //-- Date: 26 March 17 //-- #########################
- Create a macro named
Check_Val
:
macro_rules! Check_Val { (x => $e:expr) => (println!("mode X: {}", $e)); (y => $e:expr) => (println!("mode Y: {}", $e)); }
- Define the
main
function and...