为什么这个 rust HashMap 宏不再起作用? [英] Why does this rust HashMap macro no longer work?
本文介绍了为什么这个 rust HashMap 宏不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以前用过:
#[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};
但是,这似乎不再起作用.编译器报告:
However, this no longer seems to work. The compiler reports:
- 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};
^~~~~~~
宏定义发生了什么变化,使其不再起作用?
What's changed with macro definitions that makes this no longer work?
下面的基本示例工作正常:
The basic example below works fine:
macro_rules! foo(
{$T:ident} => { $T; };
)
struct Blah;
#[test]
fn test_map_create() {
let mut bar = foo!{Blah};
}
所以这似乎是对 {T:ident, $(...), +} 扩展处理方式的一些更改?
So this seem to be some change to how the {T:ident, $(...), +} expansion is processed?
这是怎么回事?
推荐答案
T
之前缺少 $
符号.
这篇关于为什么这个 rust HashMap 宏不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文