【发布时间】:2013-07-28 21:23:23
【问题描述】:
我无法弄清楚如何以惯用的方式使用带有~str 类型键的 HashMap。例如,
let mut map: hashmap::HashMap<~str, int> = hashmap::HashMap::new();
// Inserting is fine, I just have to copy the string.
map.insert("hello".to_str(), 1);
// If I look something up, do I really need to copy the string?
// This works:
map.contains_key(&"hello".to_str());
// This doesn't: as expected, I get
// error: mismatched types: expected `&~str` but found `&'static str` (expected &-ptr but found &'static str)
map.contains_key("hello");
基于this 错误报告,我尝试过
map.contains_key_equiv("hello");
但是得到了
error: mismatched types: expected `&<V367>` but found `&'static str` (expected &-ptr but found &'static str)
我真的不明白这最后一条消息;有人有什么建议吗?
【问题讨论】:
-
这个问题现在已经过时了;感谢
Borrowtrait,将代码简单翻译成当前语法和方法名称就可以正常工作,没有错误。