rust - Why do I have to use &char instead of char to index a key in a HashMap<char, i32>? - Stack Overflow
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
In the following snippet, given the type of 'a'
is char
, why can't I print letters['a']
?
use std::collections::HashMap;
fn main() {
let mut letters = HashMap::new();
for ch in "a short treatise on fungi".chars() {
letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
}
println!("{}", letters[&'a']);
}
I have tried to print the type of letters
, which is std::collections::hash::map::HashMap<char, i32>
, the type of 'a'
, which is char
, and the type of &'a'
, which is &char
.
In the following snippet, given the type of 'a'
is char
, why can't I print letters['a']
?
use std::collections::HashMap;
fn main() {
let mut letters = HashMap::new();
for ch in "a short treatise on fungi".chars() {
letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
}
println!("{}", letters[&'a']);
}
I have tried to print the type of letters
, which is std::collections::hash::map::HashMap<char, i32>
, the type of 'a'
, which is char
, and the type of &'a'
, which is &char
.
1 Answer
Reset to default 3In Rust, the subscript operator []
is implemented using the Index
trait. Index
is generic over the type doing the indexing, so impl Index<usize> for Type
means any Type
value can be indexed with any usize
value.
The impl for HashMap
looks like this:
impl<K, Q, V, S> Index<&Q> for HashMap<K, V, S>
where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash + ?Sized,
S: BuildHasher,
K
is the key type in the HashMap
, and the indexing type is &Q
. These are linked by K: Borrow<Q>
. We know K
is char
, so for which Q
does K
implement Borrow<Q>
?
impl<T> Borrow<T> for T
where
T: ?Sized,
Here, T
is char
, so the only implementation is impl Borrow<char> for char
.
- Which means when
K
ischar
,Q
is alsochar
- Which means
Index<&Q>
isIndex<&char>
- Which means you can use
&char
to indexHashMap<char, _>
This is done because we want an extensible way to index, for example, HashMap<String, _>
with &str
, since String
may be expensive to create. But unfortunately, this means we can't also have impl Index<K> for HashMap<K, _>
. So we're stuck borrowing every index, whether it makes sense or not.
- 谷歌重大突破:量子计算机或真可行
- 京东1.7亿美元投资金蝶原因:布局企业ERP市场
- 软件定义网络SDN与网络可见性的挑战
- Mac电脑与PC九大区别
- 英特尔移动之战
- 奇虎诉腾讯索赔1.5亿创天价 双方股价昨齐上涨
- c++ - Android OpenXR application java.io.FileNotFoundException: apexcom.meta.xrpriv-appVrDriverVrDriver.apk - Stack Overflow
- Why is my openGL render failing, Python OpenGL - Stack Overflow
- How can I add a Linux system-level API without recompiling the kernel? - Stack Overflow
- graphdb - vector embedding on ontotext similarity plugin - Stack Overflow
- How to optimize query performance in a large fact table with billions of rows? - Stack Overflow
- c# - I have a time clock connected to my local network and I have a Windows server on localweb and I can't get the clock
- python - Having issues getting django-simple-captcha to work on my Contact Us page - Stack Overflow
- node.js - Cookies Blocked in Cross-Origin Requests Between Netlify Frontend(Reactjs) and Railway Backend (Node ts) - Stack Overf
- swift - SwiftUI ScrollView not scrolling with DragGesture inside a ForEach Item - Stack Overflow
- imagemagick - How to add annotations in Right-To-Left (RTL) languages (like Arabic and Persian) to images using R's magi
- node.js - TypeError: Cannot read properties of undefined (reading 'server') while calling socket.io in nextjs -