小编Joe*_*man的帖子

生锈的字符串键入HashMap?

我无法弄清楚如何使用具有~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");
Run Code Online (Sandbox Code Playgroud)

根据这个错误报告,我试过了

map.contains_key_equiv("hello");
Run Code Online (Sandbox Code Playgroud)

但得到了

error: mismatched types: expected `&<V367>` but found `&'static str` …
Run Code Online (Sandbox Code Playgroud)

hashmap rust

6
推荐指数
1
解决办法
4738
查看次数

标签 统计

hashmap ×1

rust ×1