pu369com

Rust 的宏

参考https://www.cnblogs.com/praying/p/14457360.html     https://zhuanlan.zhihu.com/p/342408254 (Rust过程宏入门(一)及后续)

https://dengjianping.github.io/2019/02/28/%E5%A6%82%E4%BD%95%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E8%BF%87%E7%A8%8B%E5%AE%8F(proc-macro).html

 1 写一个声明式宏(Declarative macros)add!,main.rs:

macro_rules! add{
    // macth like arm for macro
       ($a:expr,$b:expr)=>{
    // macro expand to this code
           {
   // $a and $b will be templated using the value/variable provided to macro
               $a+$b
           }
       }
   }
   
   fn main(){
    // call to macro, $a=1 and $b=2     
      println!("{}",add!(3,2));

   }

2 过程宏有点麻烦,暂不写了

posted on 2021-08-18 12:54  pu369com  阅读(84)  评论(0编辑  收藏  举报

导航