上QQ阅读APP看书,第一时间看更新
How to do it...
In the src/bin folder, create a file called variadic.rs
Add the following code and run it with cargo run --bin variadic:
1 macro_rules! multiply {
2 // Edge case
3 ( $last:expr ) => { $last };
4
5 ( $head:expr, $($tail:expr), +) => {
6 // Recursive call
7 $head * multiply!($($tail),+)
8 };
9 }
10
11 fn main() {
12 // You can call multiply! with
13 // as many parameters as you want
14 let val = multiply!(2, 4, 8);
15 println!("2*4*8 = {}", val)
16 }