Skip to content

Instantly share code, notes, and snippets.

@hackaugusto
Created December 8, 2013 17:13
Show Gist options
  • Select an option

  • Save hackaugusto/7860387 to your computer and use it in GitHub Desktop.

Select an option

Save hackaugusto/7860387 to your computer and use it in GitHub Desktop.
Borrowed function
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