如何使用重复创建带有可选参数的 Rust 宏? [英] How do I create a Rust macro with optional parameters using repetitions?

查看:17
本文介绍了如何使用重复创建带有可选参数的 Rust 宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究 Rust 宏,但找不到任何关于重复的详细文档.我想创建带有可选参数的宏.这就是我的想法:

I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea:

macro_rules! single_opt {
    ($mand_1, $mand_2, $($opt:expr)* ) =>{
        match $opt {
            Some(x) => println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, x);
            None => single_opt!($mand_1, $mand_2, "Default");
        }
    }
}

fn main() {
    single_opt!(4,4);
}

这个示例似乎已经过时,因为我无法编译它.Rust 书只是非常简短地提到了这个主题.我如何让这个例子工作?

This example seems to be outdated, since I can not compile it. The Rust book mentions this topic just very briefly. How do I get this example to work?

推荐答案

注意:自从写了这个答案,Rust 获得了使用语法 $(tokens)? 在模式中表达可选元素的能力(在 Rust 1.32.0 中稳定).

NOTE: Since this answer was written, Rust has gained the ability to express optional elements in a pattern (stabilized in Rust 1.32.0) using the syntax $(tokens)?.

Rust 书的第一版有一个 关于宏的一章相当长,但关于重复的部分在例子上有点害羞......

The first edition of the Rust book has a rather long chapter on macros, but the section on repetitions is a bit shy on examples...

有几种方法可以处理宏中的可选参数.如果你有一个只能出现一次的可选参数,那么你不应该使用重复:你应该在你的宏中定义多个模式,像这样:

There are several ways to handle optional arguments in macros. If you have an optional argument that can only occur once, then you shouldn't use repetitions: you should instead define multiple patterns in your macro, like this:

macro_rules! single_opt {
    ($mand_1:expr, $mand_2:expr) => {
        single_opt!($mand_1, $mand_2, "Default")
    };
    ($mand_1:expr, $mand_2:expr, $opt:expr) => {
        println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, $opt)
    };
}

fn main() {
    single_opt!(4, 4);
}

如果你想允许任意数量的参数,那么你需要重复.您的原始宏不起作用,因为您将逗号放在重复之外,因此您必须将宏调用为 single_opt!(4,4,);.有关相关案例,请参阅如何在宏中允许可选的尾随逗号?.

If you want to allow an arbitrary number of arguments, then you need repetition. Your original macro doesn't work because you put the comma outside the repetition, so you'd have to invoke the macro as single_opt!(4,4,);. See How to allow optional trailing commas in macros? for a related case.

如果你有固定数量的参数后面跟着一个重复,你可以把逗号放在重复里面作为第一个标记:

If you have a fixed number of arguments followed by a repetition, you can put the comma inside the repetition as the first token:

macro_rules! single_opt {
    ($mand_1:expr, $mand_2:expr $(, $opt:expr)*) => {
        println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, $($opt),*)
    };
}

但是,它在这种特定情况下不起作用:

However, it doesn't work in this specific case:

error: 3 positional arguments in format string, but there are 2 arguments
 --> src/main.rs:3:22
  |
3 |         println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, $($opt),*)
  |                      ^^    ^^     ^^
...
8 |     single_opt!(4, 4);
  |     ------------------
  |     |
  |     in this macro invocation
  |     in this macro invocation
  |     in this macro invocation
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

所以我们必须回到定义两种模式:

So we'll have to go back to defining two patterns:

macro_rules! single_opt {
    ($mand_1:expr, $mand_2:expr) => {
        single_opt!($mand_1, $mand_2, "Default")
    };
    ($mand_1:expr, $mand_2:expr, $($opt:expr),*) => {
        {
            println!("1. {} 2. {}", $mand_1, $mand_2);
            $(
                println!("opt. {}", $opt);
            )*
        }
    };
}

fn main() {
    single_opt!(4, 4, 1, 2);
}

重复采用 $( PATTERN ) SEPARATOR COUNT 的形式,其中 PATTERN 是您要重复的模式,SEPARATOR 是可选的分隔每个重复的标记(这里是 ,),COUNT 要么是 * 表示零次或多次出现";或 + 表示一次或多次出现".

A repetition takes the form $( PATTERN ) SEPARATOR COUNT, where PATTERN is the pattern you want to repeat, SEPARATOR is an optional token that separates each repetition (here, it's ,) and COUNT is either * for "zero or more occurrences" or + for "one or more occurrences".

然后,在宏扩展中,我们需要一个重复块才能访问$opt.语法完全相同,但请注意分隔符不必相同(此处,扩展中没有分隔符).

Then, in the macro expansion, we need a repetition block to be able to access $opt. The syntax is exactly the same, but note that the separator doesn't have to be the same (here, there's no separator in the expansion).

这篇关于如何使用重复创建带有可选参数的 Rust 宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆