Follow the given steps to implement this recipe:
- Create a file named sample_repeat.rs, and open it in your text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: Implementing repeat
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 26 March 17
//-- #########################
- Create a macro named find_min, in which we implement repeat:
macro_rules! find_min {
// Base case:
($x:expr) => ($x);
// `$x` followed by at least one `$y,`
($x:expr, $($y:expr),+) => (
// Call `find_min!` on the tail `$y`
std::cmp::min($x, find_min!($($y),+))
)
}
- Create a main function in which we pass multiple arguments to find_min:
fn main() {
println!("{}", find_min!(1u32));
println!("{}", find_min!(1u32 + 2 , 2u32));
println!("{}", find_min!(5u32, 2u32 * 3, 4u32));
}
We will get the following output on the successful execution of our code: