简体   繁体   English

特性`std::borrow::Borrow<char> ` 没有为 `&amp;str` 实现

[英]the trait `std::borrow::Borrow<char>` is not implemented for `&str`

let mut map: HashMap<&str, u32> = HashMap::new();

for (i, c) in text.chars().enumerate() {
    if map.contains_key(&c) {
    // Do something
    }
}

the trait std::borrow::Borrow<char> is not implemented for &str特征std::borrow::Borrow<char>没有为&str实现

I need an explanation of this error and how to fix it please.我需要对此错误的解释以及如何修复它。 I am looping through every character in a text and inserting the ones not already in a hashmap into the hash map.我正在遍历文本中的每个字符,并将哈希图中尚未包含的字符插入到哈希图中。 But i keep getting the error as stated above.但我不断收到上述错误。

chars is an Iterator , whose Item = char , so your HashMap<&str, u32> is not compatible with it. chars是一个Iterator ,其Item = char ,因此您的HashMap<&str, u32>与它不兼容。

&str is a string slice (essentialy a sequence of characters), while a char is a single character. &str是一个字符串切片(本质上是一个字符序列),而一个char是单个字符。

You must decide:您必须决定:

  • Should map really map from &str to u32 ? map真的应该从&str映射到u32吗? Or possibly from char ?或者可能来自char Or possibly from &char ?或者可能来自&char
  • If you go for &str , you must convert chars 's elements to &str .如果您选择&str ,则必须将chars的元素转换为&str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM