首页 > 其他分享 >[Rust] Write macro

[Rust] Write macro

时间:2024-02-27 15:35:50浏览次数:18  
标签:val macro Write println main my Rust out

Define a macro and use it:

macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    };
}

fn main() {
    my_macro!();
}

 

Notice that you have time define macrobefore main function. Otherwise it doesn't work.

 

Expose a macro from module:

mod macros {
    #[macro_export]
    macro_rules! my_macro {
        () => {
            println!("Check out my macro!");
        };
    }
}

fn main() {
    my_macro!();
}

We need to add attribute #[macro_export], in order to make it work.

 

Arms:

The macro my_macro is defined with two "arms", each of which matches different patterns of input and associates those patterns with specific code blocks.

There are two arms in following code:

macro_rules! my_macro {
    () => {
        println!("Check out my macro!");
    }; // ; is important
    ($val:expr) => { //:expr the val should be an expersion
        println!("Look at this other macro: {}", $val);
    }; // ; is important
}

fn main() {
    my_macro!();
    my_macro!(7777);
}

 

标签:val,macro,Write,println,main,my,Rust,out
From: https://www.cnblogs.com/Answer1215/p/18036962

相关文章

  • Rust 无畏并发
    本文在原文基础上有删减,原文链接无畏并发。目录使用线程同时运行代码使用spawn创建新线程使用join等待所有线程结束将move闭包与线程一同使用使用消息传递在线程间传送数据信道与所有权转移发送多个值并观察接收者的等待通过克隆发送者来创建多个生产者共享状态并发互斥器......
  • [Rust] module with public and private methods
    Methods:modsausage_factory{//privatemethodfnget_secret_recipe()->String{String::from("Ginger")}//publicmethodpubfnmake_sausage(){get_secret_recipe();println!("sausage!&qu......
  • Rust开发日记
    Gettingstarted-RustProgrammingLanguage(rust-lang.org)  安装好配置环境变量Path:%CARGO_HOME%和%RUSTUP_HOME% 建立config文件,不要扩展名。[source.crates-io]registry="https://github.com/rust-lang/crates.io-index"#替换成你偏好的镜像源replace-......
  • 《安富莱嵌入式周报》第333期:F35战斗机软件使用编程语言占比,开源10V基准电源,不断电运
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 视频版:https://www.bilibili.com/video/BV1y1421f7ip目录:1、F35战斗机软件使用编程语言占比2、开源10V基准电源,不断电运行一年,误差小于1uV3、资讯(1)苹果开源配置语言Pkl......
  • 基于Rust的Tile-Based游戏开发杂记(01)导入
    什么是Tile-Based游戏?Tile-based游戏是一种使用tile(译为:瓦片,瓷砖)作为基本构建单位来设计游戏关卡、地图或其他视觉元素的游戏类型。在这样的游戏中,游戏世界的背景、地形、环境等都是由一系列预先定义好的小图片(即tiles)拼接而成的网格状结构。每个tile通常代表一个固定的尺寸区域,......
  • 多线程系列(十) -ReadWriteLock用法详解
    一、摘要在上篇文章中,我们讲到ReentrantLock可以保证了只有一个线程能执行加锁的代码。但是有些时候,这种保护显的有点过头,比如下面这个方法,它仅仅就是只读取数据,不修改数据,它实际上允许多个线程同时调用的。publicclassCounter{privatefinalLocklock=newReentra......
  • pd.ExcelWriter 实现数据写入不同sheet
    pd.ExcelWriter将数据写入不同sheet当结合for循环使用时,需注意放在for循环前面以下写法,仅生成一个sheet,原因在于pd.ExcelWriter的mode默认是w,每次for循环写入数据都会对原有的数据进行覆盖,最终只会生成一个sheet。importpandasaspddf1=pd.DataFrame([["AAA","BBB"]],......
  • [Rust] impl TryFrom and try_into()
    TheTryFromandtry_into()methodsarepartofthe.standardlibaray'sconversiontraits,designedtohandleconversionsbetweentypesinafalliblemanner-thatis,conversionsthatmightfail.Thisisincontrastto`From`and`into()`methods,wh......
  • [Rust] Instantiating Classic, Tuple, and Unit structs
    https://doc.rust-lang.org/book/ch05-01-defining-structs.html structColorClassicStruct{red:i32,green:i32,blue:i32}structColorTupleStruct(i32,i32,i32);#[derive(Debug)]structUnitLikeStruct;#[cfg(test)]modtests{usesu......
  • [Rust] Create an array of numbers by using Range
    fnmain(){leta=0..100;ifa.len()>=100{println!("Wow,that'sabigarray!");}else{println!("Meh,Ieatarrayslikethatforbreakfast.");panic!("Arraynotbigenough,more......