From 541d06afd8138094783634ab92f6091313029f84 Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Mon, 3 Nov 2025 03:28:22 +0500 Subject: [PATCH] nits --- code/examples/misc_7.rs | 7 ++++--- code/examples/stderr/misc_7.stderr | 2 ++ src/misc/7.md | 9 +++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/code/examples/misc_7.rs b/code/examples/misc_7.rs index a39ef02..ae329a1 100644 --- a/code/examples/misc_7.rs +++ b/code/examples/misc_7.rs @@ -14,6 +14,7 @@ macro_rules! transform { } fn main() { - let x = capture!(2 + 3); - println!("{}", x); // what this will print? -} \ No newline at end of file + // what will be output of this macro calls + dbg!(capture!(2 + 3)); + dbg!(transform!(2 + 3)); +} diff --git a/code/examples/stderr/misc_7.stderr b/code/examples/stderr/misc_7.stderr index e69de29..156668c 100644 --- a/code/examples/stderr/misc_7.stderr +++ b/code/examples/stderr/misc_7.stderr @@ -0,0 +1,2 @@ +[examples/misc_7.rs:18:5] capture!(2 + 3) = 5 +[examples/misc_7.rs:19:5] transform!(2 + 3) = 6 diff --git a/src/misc/7.md b/src/misc/7.md index b03dc13..a4303bc 100644 --- a/src/misc/7.md +++ b/src/misc/7.md @@ -9,9 +9,14 @@
Solution -It will prints `5`. +The actual output will be: -Once a token is captured as an expression (`:expr`), it becomes opaque to macro pattern matching and cannot be destructured into its component tokens. +``` +[src/main.rs:17:5] capture!(2 + 3) = 5 +[src/main.rs:18:5] transform!(2 + 3) = 6 +``` + +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. 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`.