我以前用过:
#[macro_export]
macro_rules! map(
{ T:ident, $($key:expr => $value:expr),+ } => {
{
let mut m = $T::new();
$(
m.insert($key, $value);
)+
m
}
};
)
创建对象,像这样:
let mut hm = map! {HashMap, "a string" => 21, "another string" => 33};
然而,这似乎不再有效。编译器报告:
- Failed:
macros.rs:136:24: 136:31 error: no rules expected the token `HashMap`
macros.rs:136 let mut hm = map! {HashMap, "a string" => 21, "another string" => 33};
^~~~~~~
宏定义有什么变化使它不再有效?
下面的基本示例工作正常:
macro_rules! foo(
{$T:ident} => { $T; };
)
struct Blah;
#[test]
fn test_map_create() {
let mut bar = foo!{Blah};
}
所以这似乎是对 {T:ident, $(...), +} 扩展处理方式的一些改变?
这是怎么回事?
最佳答案
您在 T
之前缺少 $
符号。
关于rust - 为什么这个 rust HashMap 宏不再起作用了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24276617/