Skip to content

Instantly share code, notes, and snippets.

@pzol
Created February 3, 2014 19:05
Show Gist options
  • Save pzol/8790126 to your computer and use it in GitHub Desktop.
Save pzol/8790126 to your computer and use it in GitHub Desktop.
use std::hashmap::HashMap;
#[test]
fn test_mangle(){
let mut map = HashMap::<~str, uint>::new();
assert!(!map.contains_key(&~"foo"));
map.mangle(~"foo", 1,
|_k, a| a + 10,
|_k, v, a| *v -= 2
);
assert_eq!(*map.get(&~"foo"), 11);
// doesn't borrow map
let x = *map.mangle(~"foo", 1,
|_k, a| a + 10,
|_k, v, _a| *v -= 2
);
assert_eq!(*map.get(&~"foo"), 9);
// borrow map
let y = map.mangle(~"foo", 1,
|_k, a| a + 10,
|_k, v, _a| *v -= 2
);
// error here
assert_eq!(*map.get(&~"foo"), 7);
}
@pzol
Copy link
Author

pzol commented Feb 3, 2014

/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:28:15: 28:18 error: cannot borrow map because it is already borrowed as mutable
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:28 assert_eq!(*map.get(&"foo"), 7);
^~~
:1:1: 1:1 note: in expansion of assert_eq!
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:28:3: 28:36 note: expansion site
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:22:11: 22:14 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/map_mangle.rs:22 let y = map.mangle(
"foo", 1,
^~~
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:30:2: 30:2 note: previous borrow ends here
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:4 fn test_mangle(){
...
/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:30 }
^
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_map_mangle', '/Users/pzol/Dropbox/src/rust/one/src/map_mangle.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