You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a HashMap<String, Foo>, just as a lookup can be performed with an &str parameter, it should be possible to obtain a HashMap::Entry<K, V> without requiring a full-blown instance of K be passed in to .entry(...).
i.e. the following code should work:
use std::collections::HashMap;fnmain(){letmut map = HashMap::new();
map.insert("Foo".to_owned(),"Bar".to_owned());{// read-only reference to the result:let bar = map.get("Foo");assert!(bar.is_some());}{// why should this not be allowed?let bar = map.entry("Foo");}}
Which currently returns the following:
error[E0308]: mismatched types
--> ./test.rs:16:29
|
16 | let bar = map.entry("Foo");
| ^^^^^
| |
| expected struct `std::string::String`, found reference
| help: try using a conversion method: `"Foo".to_string()`
|
= note: expected type `std::string::String`
found type `&'static str`
error: aborting due to previous error
The text was updated successfully, but these errors were encountered:
I don't see why the
HashMap.entry(..)
methodrequires that the lookup parameter
key
be of typeK
and not some typeQ
whereK: Borrow<Q>
, akin toHashMap.get(..)
:Given a
HashMap<String, Foo>
, just as a lookup can be performed with an&str
parameter, it should be possible to obtain aHashMap::Entry<K, V>
without requiring a full-blown instance ofK
be passed in to.entry(...)
.i.e. the following code should work:
Which currently returns the following:
The text was updated successfully, but these errors were encountered: