Last active
December 17, 2015 03:39
-
-
Save DaGenix/5545095 to your computer and use it in GitHub Desktop.
Rust code that won't compile
This file contains 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
// This code works fine | |
fn get(f: ~fn() -> int) -> ~fn() -> int { | |
|| { | |
f() | |
} | |
} | |
fn main() { | |
get(|| {8})(); | |
} | |
// This code fails to compile with "error: moving out of captured outer variable in a heap closure" | |
// on "call(f)" | |
// I was thinking that the closure constructed by get() would get ownership of the closure f. I thought that it | |
// would then give up ownership of f when it invoked call(). However, my best guess is that the closure is unable to | |
// give up ownership for values its closing over? Is that correct or is something else going on here? | |
fn call(g: ~fn() -> int) -> int { | |
g() | |
} | |
fn get(f: ~fn() -> int) -> ~fn() -> int { | |
|| { | |
call(f) | |
} | |
} | |
fn main() { | |
get(|| {8})(); | |
} | |
// I tried to rewrite call() to take a borrewed closure. This code compiles, but when run, it results in: | |
// | |
// exchange heap not empty on exit | |
// 1 dangling allocations | |
// Aborted (core dumped) | |
// | |
fn call(f: &fn() -> int) -> int { | |
f() | |
} | |
fn get(f: ~fn() -> int) -> ~fn() -> int { | |
|| { | |
call(f) | |
} | |
} | |
fn main() { | |
get(|| {8})(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment