我在如何使用键类型为~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");
根据这个错误报告,我尝试了以下操作:
map.contains_key_equiv("hello");
但是收到了
error: mismatched types: expected `&<V367>` but found `&'static str` (expected &-ptr but found &'static str)
我真的不理解这个最后的消息;有人有建议吗?
Borrow
特质的存在,将代码简单地转换为当前的语法和方法名称就可以正常工作,而且没有错误。 - Chris Morgan