Created
February 3, 2014 19:05
-
-
Save pzol/8790126 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::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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/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);"foo", 1,^~~
: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 ofmap
until the borrow ends/Users/pzol/Dropbox/src/rust/one/src/map_mangle.rs:22 let y = map.mangle(
^~~
/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]