After looking at your code closer, I see why the compiler led you astray: you are trying to create a self-referential struct.
When you have a lifetime <'a>
on a struct, that lifetime denotes references to values stored outside of the struct. If you try to store a reference that points inside the struct rather than outside, you will run into a compiler error when the compiler notices you lied to it.
Use the key instead of a reference:
pub struct IndexerCustomer {
index_code: BTreeMap<u32, Customer>,
index_name: BTreeMap<String, u32>,
index_city: BTreeMap<String, u32>,
}
impl IndexerCustomer {
pub fn insert(&mut self, customer: Customer) {
let customer_opt = match self.index_code.entry(customer.code) {
Entry::Occupied(_) => None,
Entry::Vacant(e) => Some(&*e.insert(customer)),
};
match customer_opt {
Some(customer) => {
self.index_name.insert(customer.name.clone(), customer.code);
self.index_city.insert(customer.city.clone(), customer.code);
}
None => {}
}
}
}