Avoid moving value when using HashMap#get() with a tuple of String

Given a std::collection::HashMap with the following signature:

let map: HashMap<(String, String), i32>;

How to avoid moving the members of the tuple (String, String) when looking-up values using the get() method?

let foo = "ABC".to_string();
let bar = "XYZ".to_string();

map.get(&(foo, bar));

// Cannot use either foo or bar here because they were moved with get()
// call above. Is there a way?
let tmp = (foo,  bar);
map.get(&tmp);
let (foo, bar) = tmp;

The problem is, to get a reference of the tuple of strings, the real tuple that owns strings should be constructed. In your code the tuple is temporal value which isn't assigned to any binding so it will be dropped at the end of the statement.

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.