Last active
May 9, 2017 15:12
-
-
Save fotcorn/6ad36699dfd3dc4919ebeb3ce90f91cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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::hash_map::HashMap; | |
struct MyStruct { | |
a: u64, | |
b: u64, | |
} | |
struct Test { | |
counter: u64, | |
map: HashMap<u64, MyStruct>, | |
} | |
impl Test { | |
pub fn new() -> Test { | |
Test { | |
counter: 0, | |
map: HashMap::new(), | |
} | |
} | |
pub fn add_if_not_yet_exists(&mut self, key: u64, a: u64, b: u64) { | |
self.map.entry(key).or_insert(self.add(a, b)); | |
} | |
pub fn add(&mut self, a: u64, b: u64) -> MyStruct { | |
self.counter += 1; | |
MyStruct {a: a, b: b} | |
} | |
} | |
fn main() { | |
let mut test = Test::new(); | |
test.add_if_not_yet_exists(1, 2, 3); | |
} | |
/* | |
error[E0499]: cannot borrow `*self` as mutable more than once at a time | |
--> src/bin/test.rs:22:39 | |
| | |
22 | self.map.entry(key).or_insert(self.add(a, b)); | |
| -------- ^^^^ - first borrow ends here | |
| | | | |
| | second mutable borrow occurs here | |
| first mutable borrow occurs here | |
error: aborting due to previous error | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment