I am writing a service that will collect a great number of values and build large structures around them. For some of these, lookup tables are needed, and due to memory constraints I do not want to copy the key or value passed to the HashMap
. However, using references gets me into trouble with the borrow checker (see example below). What is the preferred way of working with run-time created instances?
use std::collections::HashMap;
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
struct LargeKey;
struct LargeValue;
fn main() {
let mut lots_of_lookups: HashMap<&LargeKey, &LargeValue> = HashMap::new();
let run_time_created_key = LargeKey;
let run_time_created_value = LargeValue;
lots_of_lookups.insert(&run_time_created_key, &run_time_created_value);
lots_of_lookups.clear();
}
I was expecting clear()
to release the borrows, but even if it actually does so, perhaps the compiler cannot figure that out?