Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look up key in HashMap<(String, usize), f64> in Rust [duplicate]

Tags:

rust

I have a HashMap<(String, usize), f64>. I also have a &str and a usize, which I would like to look up in this HashMap without cloning. Is there a way to lookup a (&str, usize) as a (String, usize) somehow?

like image 351
yong Avatar asked Jun 15 '16 08:06

yong


1 Answers

No, you can't. The options for looking up in the HashMap<K,V> are:

  • The entry method, which requires a K by value, which in your case is a (String, usize) - so you'd need to construct a String.
  • The various get, contains_key etc. all take a "borrowed" form of K (the documentation says &Q, where K: Borrow<Q>; this means either a &(String, usize) or something that can produced one.

Technically you could iterate through it and do your own comparisons, but that's probably not what you want!

like image 184
Chris Emerson Avatar answered Sep 30 '22 21:09

Chris Emerson