rust - 有没有办法使用宏定义“匹配”标签?
问题描述
我的意思是,看代码
macro_rules! impls {
() => {
let a = "Hello, world!";
println!("{}", match 7 {
impls!{ 3 + 4, a }
_ => unimplemented!()
})
};
($x:expr, $name:ident) => {
($x) => $name,
}
}
fn main() {
impls!{ }
}
正如我所想,impls!{ 3 + 4, a }
应该是(3 + 4) => a
,但事实并非如此。怎么了?编译器错误:
error: expected one of `=>`, `if`, or `|`, found reserved identifier `_`
--> src/main.rs:6:13
|
5 | impls!{ 3 + 4, a }
| - expected one of `=>`, `if`, or `|`
6 | _ => unimplemented!()
| ^ unexpected token
...
16 | impls!{ }
| --------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
解决方案
这对于声明性宏是不可能的(可能是过程宏,我对这些宏的经验很少,所以我不知道):根据文档
宏可以扩展到表达式、语句、项目(包括特征、实现和外来项目)、类型或模式。
匹配臂本身并不是“Rust 构造”,它是表达式的子项(match
由模式、可选的保护表达式和主体表达式组成)。
因此,它不能由声明性宏生成。
推荐阅读
- python-3.x - 爬取wiki中的第一段链接
- c - 为什么我的 freeMem 函数需要 char* 和 addMem 函数 char**
- elasticsearch - 查询 ElasticSearch - 在不同时间匹配的多个术语
- node.js - Mongoose 独特的字段处理
- sockets - 为什么 Haskell 中的 System.Timeout 不适用于套接字?
- r - 如何在多个 blogdown 帖子中加载相同的所需包?
- java - Android Intent Service APP在后台静默工作仍以白屏启动
- c# - Startup.cs 中 SPA 源路径的 GetCurrentDirectory() 正在为 VS2017 和 VS Code 返回不同的路径
- android - 在片段上的 getCurrentUser().getUid() 上获取空对象引用
- sql - 分组数据问题