Skip to content

Instantly share code, notes, and snippets.

@quux00
Last active January 3, 2016 13:09
Show Gist options
  • Save quux00/8467756 to your computer and use it in GitHub Desktop.
Save quux00/8467756 to your computer and use it in GitHub Desktop.
// Trying to learn closures/procs in Rust // Want to capture a variable whose private state I can mutate // Currently failing
//
// Trying to learn closures/procs in Rust
// Want to capture a variable whose private state I can mutate
//
fn main() {
fn call_proc(b: proc(u64) -> u64) {
let x = b(10u64);
println!("{:?}", x);
}
let p = get_proc();
call_proc(p);
call_proc(p); // error: use of moved value: `p`
// compiler note: `p` moved here because it has type `proc:Send(u64) -> u64`,
// which is non-copyable (perhaps you meant to use clone()?)
}
fn get_proc() -> proc(u64) -> u64 {
let count: ~u64 = ~0;
proc(N: u64) -> u64 {
let mut counter = count;
let val: u64 = N + *counter;
*counter += 1;
return val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment