Skip to content

Instantly share code, notes, and snippets.

@muhamadazmy
Created June 10, 2022 11:02
Show Gist options
  • Save muhamadazmy/a9ce703a640fb6b025f0a3cb0e48883d to your computer and use it in GitHub Desktop.
Save muhamadazmy/a9ce703a640fb6b025f0a3cb0e48883d to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
struct Module;
struct Data {
modules: HashMap<String, Module>,
}
impl Data {
fn module<S: AsRef<str>>(&mut self, name: S) -> &mut Module {
if let Some(m) = self.modules.get_mut(name.as_ref()) {
return m;
}
// shouldn't the borrow of mut self end here because the object
// did not found in the map, hence the following statements should work
self.modules.insert(name.as_ref().into(), Module);
self.modules.get_mut(name.as_ref()).unwrap()
}
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment