I want to create a hash map from a vector of entities. I want the key to be a reference to a field in the corresponding value. This is something I have come up with.
struct Entity {
id: String,
patterns: Vec<Pattern>,
}
struct Package<'ent> {
entity_map: HashMap<&'ent String, Entity>,
}
impl<'ent> Package<'ent> {
fn from(entities: Vec<Entity>) -> Self {
let entity_map: HashMap<&String, Entity> =
entities.into_iter().map(|e| (&e.id, e)).collect();
Package { entity_map }
}
}
Of course, this isn't working. I am pretty new to Rust. Is there anything wrong with this approach? How can I achieve what I want? Any help will be appreciated.