I have a Record
type, with a get_key(&self) -> &str
method (borrowing the returned str
from inside the record). Records may be mutable, but their key is guaranteed not to change.
I want to store a list of records in a collection, and be able to retrieve a record by its key. The first idea is to use a HashMap<String, Record>
, but that looks like a waste of space (because the strings in the key position are copies of the record's keys).
I would rather have a HashMap<&str, Record>
, but I can't provide a lifetime for the &str
keys (since they are borrowed from inside the hashmap). This is the kind of self-reference that crates like rental or owning_ref are designed to handle. However, none of them provide an out-of-the-box solution for this use-case.
I looked for a crate providing this kind of "indexed collection", but to no avail. Am I missing something? (I started hacking a solution of my own, but I'd rather not re-invent the wheel)