Last active
January 3, 2016 13:09
-
-
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
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
// | |
// 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