Problem with macro

Wrote little macro for printing

pub fn pt<'a, S: AsRef<str>>(data: S, text: Option<S>)
where S: Into<Cow<'a, str>>{
    println!("{}, {:>?}", data.into(), text.as_ref().map(|r| r.as_ref().to_string()));
}
macro_rules! pt {
    ($a: expr) => {
        pt(a, None)
    };
    ($a: expr, $b: expr) =>{
       pt(a , b)};
}

Error: cannot find value a in this scope.
Also, can someone explain how to use tt, item and block in macro expression, thanks!

You need to use $a and $b inside the macro body too instead of a and b.

1 Like

So write in both arms ($a: expr, $b: expr)?

This is what I mean:

macro_rules! pt {
    ($a: expr) => {
        pt($a, None)
    };
    ($a: expr, $b: expr) =>{
       pt($a , $b)};
}
2 Likes