Skip to content

Commit 786f15f

Browse files
committed
nits
1 parent 97e669f commit 786f15f

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

code/examples/misc_7.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ macro_rules! transform {
1414
}
1515

1616
fn main() {
17-
let x = capture!(2 + 3);
18-
println!("{}", x); // what this will print?
19-
}
17+
// what will be output of this macro calls
18+
dbg!(capture!(2 + 3));
19+
dbg!(transform!(2 + 3));
20+
}

code/examples/stderr/misc_7.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[src/main.rs:17:5] capture!(2 + 3) = 5
2+
[src/main.rs:18:5] transform!(2 + 3) = 6

src/misc/7.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
<details>
1010
<summary>Solution</summary>
1111

12-
It will prints `5`.
12+
The actual output will be:
1313

14-
Once a token is captured as an expression (`:expr`), it becomes opaque to macro pattern matching and cannot be destructured into its component tokens.
14+
```
15+
[src/main.rs:17:5] capture!(2 + 3) = 5
16+
[src/main.rs:18:5] transform!(2 + 3) = 6
17+
```
18+
19+
Because once a token is captured as an expression (`:expr`), it becomes opaque to macro pattern matching and cannot be destructured into its component tokens.
1520

1621
When `capture!(2 + 3)` is invoked, the `$e:expr` matcher captures `2 + 3` as a single expression token tree. This captured expression is then passed to `transform!($e)`, which attempts to match it against the pattern `$a:tt + $b:tt`.
1722

0 commit comments

Comments
 (0)