Macro match value errror

I am working with the below macro. I need a help to extract the value inside the matched pattern.

macro_rules! match_value {
    ($p:pat, $v:expr) => {
        match $v {
            $p => println!("the pattern matched."),
            _ => println!("the pattern does not match."),
        }
    };
}

fn main() {
    let value = Some(42);
    match_value!(Some(_), value); 
    match_value!(None, value);
}

not sure what you are trying to do, but I think your code example doesn't contain any errors.

if you want to check if a value matches a given pattern (you want to get a boolean result), there's already one macro in the standard library for that:

if that's not what you mean, please be more specific or add more examples to clarify what problem you are trying to solve.

1 Like