Skip to content

Instantly share code, notes, and snippets.

Created November 20, 2015 05:52
Show Gist options
  • Save anonymous/aa96e9e0fe78a09af2eb to your computer and use it in GitHub Desktop.
Save anonymous/aa96e9e0fe78a09af2eb to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![feature(fnbox)]
use std::boxed::FnBox;
struct Foo;
impl Foo {
pub fn new() -> Foo {
println!("foo: new");
Foo
}
pub fn dowork(&self) {
println!("foo: doing work");
}
}
impl Drop for Foo {
fn drop(&mut self) {
println!("foo: drop");
}
}
fn process(foo: Foo) -> Box<FnBox()> {
Box::new(move || {
foo.dowork();
/* info: foo is never dropped
* at the end of this closure despite
* being owned by the closure, this
* requires the caller to explicitly
* drop() captured members.
*/
// uncomment
// drop(foo); // explicit drop!!
})
}
fn main() {
let f = process(Foo::new());
f();
println!("main: exit");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment