Created
June 10, 2022 11:02
-
-
Save muhamadazmy/a9ce703a640fb6b025f0a3cb0e48883d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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