Rust Macro Does Not Match Passed Types

The types passed directly to the macro template correspond to what you expected, but if they are passed through another macro as ty, they no longer match:

macro_rules! mrtype {
    ( bool )   => ("b");
    ( i32 )    => ("i");
    ( f64 )    => ("f");
    ( &str )   => ("z");
    ( $_t:ty ) => ("o");
}

macro_rules! around {
    ( $t:ty ) => (mrtype!($t));
}

fn main() {
    println!("{}{}{}", mrtype!(i32), around!(i32), around!(&str));
}

Prints iooinstead iiz.

Transmission ttinstead tyworks, but if you have one &str, you need 2 tts, which makes everything unnecessarily complicated.

+4
source share
1 answer

This does not work and cannot work.

Captures and Expansion Redux " " : , tt ident macro_rules! . - ty, macro_rules!.

: &str macro_rules!: , & str. , &str ty, , "-" : &str. , , , .

, tt ident ( ). around be ($($t:tt)*) => (mrtype!($($t)*));, .

+7

Source: https://habr.com/ru/post/1629774/


All Articles