Skip to content

Commit 82fc107

Browse files
committed
macros: Add test cases for recursive macro invocation
1 parent c849983 commit 82fc107

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:literal) => {
3+
0 + $e
4+
};
5+
($e:literal $($es:literal)*) => {
6+
$e + add!($($es)*)
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(1 2 3 10); // 16
12+
13+
a - 16
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! two {
2+
(2) => {
3+
3
4+
};
5+
}
6+
7+
macro_rules! one {
8+
(1) => {
9+
two!(2)
10+
};
11+
}
12+
13+
fn main() -> i32 {
14+
let a = one!(1);
15+
16+
a - 3
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:literal) => {
3+
0 + $e
4+
};
5+
($e:literal $($es:literal)*) => {
6+
$e + add!($($es)*)
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(3 4); // 7
12+
13+
a - 7
14+
}

0 commit comments

Comments
 (0)