首页 > 其他 > 详细

Rust 的宏

时间:2021-08-18 15:17:42      阅读:13      评论:0      收藏:0      [点我收藏+]

参考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 过程宏有点麻烦,暂不写了

Rust 的宏

原文:https://www.cnblogs.com/pu369/p/15156279.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!