[英]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:您必须决定:
map
really map from &str
to u32
? map
真的应该从&str
映射到u32
吗? Or possibly from char
?或者可能来自char
? Or possibly from &char
?或者可能来自&char
?&str
, you must convert chars
's elements to &str
.如果您选择&str
,则必须将chars
的元素转换为&str
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.