实际上不可能创建这样的宏还是我做错了:
sample!("hello", "there") =>
println!("{:?}", "hello");
println!("{:?}", "there");
sample!("hello", "there", a "second type", a "another second type") =>
println!("{:?}", "hello");
println!("{:?}", "there");
println!("second {:?}", "second type");
println!("second {:?}", "another second type");
Run Code Online (Sandbox Code Playgroud)
我试过的是这个(游乐场链接):
macro_rules! sample {
(
$( $first:literal ),*
$( a $second:literal ),*
) => {
$(
println!("{:?}", $first);
)*
$(
println!("second {:?}", $second);
)*
};
}
Run Code Online (Sandbox Code Playgroud)
哪个失败了:
error: no rules expected the token `a`
--> main.rs:18:20
|
1 | macro_rules! sample {
| ------------------- when calling this macro
...
18 | sample!("hello", a "testing");
| ^ no rules expected this token in macro call
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
Rust 宏对它们的分隔符非常严格。
macro_rules! sample {
(
$( $first:literal, )*
$( a $second:literal ),*
) => {
$(println!("{:?}", $first);)*
$(println!("second {:?}", $second);)*
};
}
fn main() {
sample!("hello", a "testing");
}
Run Code Online (Sandbox Code Playgroud)
这个示例有效,你能发现变化吗?我将逗号从第一个外部移到了$( ... )内部。区别在于:
$( $a:literal ),*只接受"a", "b", "c" (不允许尾随逗号)$( $a:literal, )*只接受"a", "b", "c", (需要尾随逗号)在您的宏中,中间的逗号与第一次或第二次重复的一部分不匹配。错误基本上是说它期望另一个$first而不是一个,$second因为这就是重复所说的。
您可以通过引入一个可选的逗号来修复它:
macro_rules! sample {
(
$( $first:literal ),*
$(,)? // <----------------
$( a $second:literal ),*
) => {
$(println!("{:?}", $first);)*
$(println!("second {:?}", $second);)*
};
}
Run Code Online (Sandbox Code Playgroud)
这更宽松,但会允许像这样奇怪的事情,这可能会也可能不会,这取决于你想要什么。
sample!("hello", "there",);
sample!(, a "testing");
sample!("hello" a "testing");
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果不使用不同的手臂,我不知道一个完美的解决方案,如下所示:
macro_rules! sample {
($( $first:literal ),*) => { };
($( $first:literal, )* $( a $second:literal ),+) => { };
($( a $second:literal ),*) => { };
}
fn main() {
sample!("hello", "there");
sample!("hello", "there", a "testing");
sample!(a "second type", a "another second type");
// sample!("hello", "there",);
// sample!(, a "testing");
// sample!("hello" a "testing");
}
Run Code Online (Sandbox Code Playgroud)
也可以看看: