See the following:
macro_rules! foo {
($input:ident, $matcher:pat_param) => {
match $input {
$matcher => Some(x),
_ => None
}
}
}
enum Foo {
X(i32),
Y
}
fn main() {
let foo = Foo::X(1);
let matched = foo!(foo, Foo::X(x));
print!("{matched:?}");
}
A compiler error happens:
error[E0425]: cannot find value `x` in this scope
--> src/main.rs:4:30
|
4 | $matcher => Some(x),
| ^ not found in this scope
...
17 | let matched = foo!(foo, Foo::X(x));
| -------------------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error
Not sure why this fails, since the macro use should effectively be:
let matched = match foo {
Foo::X(x) => Some(x),
_ => None
};
Why does pat_param
accept an identifier and then cannot actually use it?