rust - 具有相同分隔符的并排宏重复
问题描述
实际上不可能创建这样的宏还是我做错了:
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");
我试过的是这个(操场链接):
macro_rules! sample {
(
$( $first:literal ),*
$( a $second:literal ),*
) => {
$(
println!("{:?}", $first);
)*
$(
println!("second {:?}", $second);
)*
};
}
失败了:
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
解决方案
Rust 宏对分隔符非常严格。
macro_rules! sample {
(
$( $first:literal, )*
$( a $second:literal ),*
) => {
$(println!("{:?}", $first);)*
$(println!("second {:?}", $second);)*
};
}
fn main() {
sample!("hello", a "testing");
}
这个样本有效,你能发现变化吗?我将逗号从第一个外部$( ... )
移到内部。区别在于:
$( $a:literal ),*
仅接受"a", "b", "c"
(不允许尾随逗号)$( $a:literal, )*
仅接受"a", "b", "c",
(需要尾随逗号)
在您的宏中,中间逗号不匹配作为第一次或第二次重复的一部分。该错误基本上是说它期望另一个$first
而不是a $second
,因为这就是重复所说的。
您可以通过引入可选逗号来修复它:
macro_rules! sample {
(
$( $first:literal ),*
$(,)? // <----------------
$( a $second:literal ),*
) => {
$(println!("{:?}", $first);)*
$(println!("second {:?}", $second);)*
};
}
这更宽松,但会允许像这样的奇怪事情,这可能会也可能不会,这取决于你想要什么。
sample!("hello", "there",);
sample!(, a "testing");
sample!("hello" a "testing");
不幸的是,如果不使用不同的手臂,我不知道一个完美的解决方案:
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");
}
也可以看看:
推荐阅读
- javascript - 使用属性值更改背景图像
- amazon-web-services - AWS CodePipeline、CodeDeploy、SAM 和 Lambda:如何(相互)连接它们?
- python - 展开打印的元组
- python - 在 scikit learn 中使用光谱双聚类之前的标准缩放数据?
- vb.net - vb.net 中 winword.exe 的多个实例
- php - 查询未正确检查一个字段
- c# - 如何根据条件阻止 TransformBlock 处理剩余的排队消息?
- python - 在 Python 中加载 JSON 时出现 TypeError
- java - 在不同的 .java 文件上按下 GUI 按钮时如何执行某些任务?
- r - 如何处理 R 中的这个 nloptr 错误?