我以前用过:
#[macro_export]
macro_rules! map(
{ T:ident, $($key:expr => $value:expr),+ } => {
{
let mut m = $T::new();
$(
m.insert($key, $value);
)+
m
}
};
)
Run Code Online (Sandbox Code Playgroud)
要创建对象,如下所示:
let mut hm = map! {HashMap, "a string" => 21, "another string" => 33};
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不再起作用.编译器报告:
- 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};
^~~~~~~
Run Code Online (Sandbox Code Playgroud)
宏定义有什么变化使得它不再起作用?
下面的基本示例运行正常:
macro_rules! foo(
{$T:ident} => { $T; };
)
struct Blah;
#[test]
fn test_map_create() {
let mut bar = foo!{Blah};
}
Run Code Online (Sandbox Code Playgroud)
所以这似乎是对{T:ident,$(...),+}扩展如何处理的一些改变?
这里发生了什么?