我無法弄清楚如何使用類型爲~str
的密鑰的HashMap。例如,生鏽的字符串鍵控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 bug報告,我想
map.contains_key_equiv("hello");
,但得到
error: mismatched types: expected `&<V367>` but found `&'static str` (expected &-ptr but found &'static str)
我真的不明白這最後的消息;有沒有人有建議?