【Rust】マクロの基本

マクロはコンパイラとは別に用意されたプリプロセッサにより簡単な文字列置換が行われる
“=>”の左側をマッチャーと呼び、右側に展開される。

macro_rules! foo {
    ($x:expr) => { println!("{}", $x);}
}

fn main(){
    foo!(1 + 2);
}

Running `target/debug/sample`
3

### matcherに使う指定子(disanator)
item, block, stmt, pat(パターン), expr(式), ty(型), ident(識別子), path, tt(トークン木), meta(アトリビュートの中身)

macro_rules! calc {
    ($x:expr, $y:expr, $op:tt) => { 
        println!("{}", $x $op $y);
    }
}

fn main(){
    calc!(1000, 5, /);
}

Running `target/debug/sample`
200

おおお、これは凄い