This code does not compile with errors. But if I replace the path!
macro with what it exactly expands to then it perfectly works. I tried with the simpler macro and it worked.
error[E0425]: cannot find value `s` in this scope
--> src/main.rs:31:27
|
31 | path!(int 10, str s) => {
| ^ help: a local variable with a similar name exists: `p`
error[E0425]: cannot find value `s` in this scope
--> src/main.rs:32:28
|
32 | println!("yes {s:?}");
| ^ help: a local variable with a similar name exists: `p`
error: arbitrary expressions aren't allowed in patterns
--> src/main.rs:31:27
|
31 | path!(int 10, str s) => {
| ^
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum PathElement {
Int(i32),
String(&'static str),
}
macro_rules! path_element {
(str $element:expr) => {
PathElement::String($element)
};
(int $element:expr) => {
PathElement::Int($element)
};
}
macro_rules! path {
($($typ:ident $element:tt), +) => (
[
$(
path_element!($typ $element),
)+
]
);
}
fn main() {
let p = path!(int 10, str "hello");
println!("{:?}", p);
match p {
//[PathElement::Int(10), PathElement::String(s)] => {
path!(int 10, str s) => {
println!("yes {s:?}");
}
_ => {
println!("no");
}
}
}