Created
December 8, 2013 17:13
-
-
Save hackaugusto/7860387 to your computer and use it in GitHub Desktop.
Borrowed function
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
| fn inner(f: &fn(&mut int)) { | |
| let mut a = ~1; | |
| f(a); | |
| } | |
| fn borrow(b: &mut int, f: &fn(&mut int)) { | |
| f(b); | |
| f(b); // can reuse borrowed variable | |
| inner(f); // shouldn't f be borrowed? | |
| // Why can't I reuse the borrowed reference to a function? | |
| // ** error: use of moved value: `f` ** | |
| //f(b); | |
| } | |
| fn main() { | |
| let mut a = ~1; | |
| print!("{}", (*a)); | |
| borrow(a, |x: &mut int| *x+=1); | |
| print!("{}", (*a)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment