我之前使用的是:
#[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, $(...), +} 扩展如何处理的一些更改?
这里发生了什么?