Looking up item in a hashtable where the key is a tuple which contains a non-copyable type

Viewed 98

Let's say I have a hashtable like this:

let table: HashMap<(usize, Vec<i32>), SomeStruct> = HashMap::new();

(in the actual application, the type is not Vec<i32> but is some other struct which is not Copy)

The key type is (usize, Vec<i32>). However, if I try to perform a lookup where I only have a (usize, &Vec<i32>), this is not allowed since HashMap::get requires Borrow<(usize, Vec<i32>)> which cannot be satisfied by (usize, &Vec<i32>).

The context looks something like this:

fn lookup(table: &mut HashMap<(usize, Vec<i32>), SomeStruct>, index: usize, vector: &Vec<i32>) {
  // Note the 'vector.clone()' - would like to avoid doing that
  if let Some(value) = table.get(&(index, vector.clone())) { 
    // ..
  }
}

Is there a way to avoid the .clone() when performing a lookup with this key type? I was thinking a struct wrapper type around the key type might allow me to write a custom implementation of Borrow but wasn't too sure if this was on the right track or how that would work.

0 Answers
Related