Rust中的macro_rules

青云 / 2024-10-15 / 原文

前言

在 Rust 中,macro_rules! 是一种声明宏,允许在编译时生成代码。

一、打印文本

示例: macro_rules!:声明了一个宏,宏的名字是 hello。调用 hello!() 时,编译器在编译时会展开宏,生成 println!("Hello, world!");

macro_rules! hello {
    () => {
        println!("Hello, world!");
    };
}

fn main() {
    hello!();  // 调用宏
}

二、带参数的宏

示例: $num:expr:宏参数 $num 是一个表达式(expr 是 Rust 中的模式类型,用于匹配任意表达式)。

macro_rules! print_num {
    ($num:expr) => {
        println!("The number is: {}", $num);
    };
}

fn main() {
    print_num!(10);  // 输出:The number is: 10
}

三、带多个参数的宏

示例: $($x:expr),:$x 匹配表达式,$() 表示重复匹配模式, 表示可以匹配零个或多个这样的表达式。

macro_rules! sum {
    ($($x:expr),*) => {
        {
            let mut total = 0;
            $(
                total += $x;
            )*
            total
        }
    };
}

fn main() {
    let result = sum!(1, 2, 3, 4, 5);  // 结果为15
    println!("Sum is: {}", result);
}

四、分支宏

示例: 根据传入的标记(first 或 second),匹配到不同的模式,生成不同的代码。

macro_rules! choose {
    (first) => {
        println!("You chose the first option.");
    };
    (second) => {
        println!("You chose the second option.");
    };
}

fn main() {
    choose!(first);   // 输出:You chose the first option.
    choose!(second);  // 输出:You chose the second option.
}

五、递归宏

示例:宏可以递归地调用自身,直到匹配结束。

macro_rules! write_list {
    () => {};  // 空列表的情况
    ($first:expr $(, $rest:expr)*) => {
        println!("{}", $first);
        write_list!($($rest),*);  // 递归调用宏
    };
}

fn main() {
    write_list!("apple", "banana", "cherry");
    // 输出:
    // apple
    // banana
    // cherry
}

六、宏中的代码块

示例: ident 是用于匹配标识符的模式。宏将根据传入的标识符(如 foo 和 bar)生成不同的函数。

macro_rules! create_function {
    ($name:ident) => {
        fn $name() {
            println!("You called {}()", stringify!($name));
        }
    };
}

create_function!(foo);
create_function!(bar);

fn main() {
    foo();  // 输出:You called foo()
    bar();  // 输出:You called bar()
}

七、总结与反思

macro_rules! 是 Rust 的声明宏,使用模式匹配来生成代码。