Skip to content

Instantly share code, notes, and snippets.

@pzol
Created January 31, 2014 11:50
Show Gist options
  • Save pzol/8730665 to your computer and use it in GitHub Desktop.
Save pzol/8730665 to your computer and use it in GitHub Desktop.
use std::hashmap::HashMap;
#[test]
fn test(){
let mut omap: Option<HashMap<~str, uint>> = None;
omap = Some(HashMap::new());
let must_insert = false;
{
let map = omap.get_mut_ref();
map.insert(~"foo", 1);
let (v, must_insert) = match map.find_mut(&~"foo") {
Some(c) => (*c, false),
None => (0 as uint, true)
};
}
if must_insert {
omap.get_mut_ref().insert(~"bar", 2);
}
}
@pzol
Copy link
Author

pzol commented Jan 31, 2014

/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:10:20: 10:23 error: cannot borrow *map because it is already borrowed as mutable
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:10 None => { map.insert("bar", 2); 2 },
^~~
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:9:17: 9:20 note: previous borrow of *map as mutable occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of *map until the borrow ends
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:9 let v = match map.find_mut(&
"foo") {
^~~
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:12:4: 12:4 note: previous borrow ends here
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:9 let v = match map.find_mut(&"foo") {
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:10 None => { map.insert(
"bar", 2); 2 },
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:11 Some(c) => 0
/Users/pzol/Dropbox/src/rust/one/src/option_test.rs:12 };
^
error: aborting due to previous error
[Finished in 0.1s with exit code 101]
[cmd: ['/usr/local/bin/rustc', '--test', '-o', '/Users/pzol/Dropbox/src/rust/one/bin/test_option_test', '/Users/pzol/Dropbox/src/rust/one/src/option_test.rs']]
[dir: /Users/pzol/Dropbox/src/rust/one/src]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment