Skip to content

Instantly share code, notes, and snippets.

@jld
Created December 23, 2015 18:20
Show Gist options
  • Select an option

  • Save jld/e96702a18478a1c8aa8c to your computer and use it in GitHub Desktop.

Select an option

Save jld/e96702a18478a1c8aa8c to your computer and use it in GitHub Desktop.
use std::thread;
struct Bees;
impl Drop for Bees {
fn drop(&mut self) { panic!("Not the bees!"); }
}
struct Msg(&'static str);
impl Drop for Msg {
fn drop(&mut self) { println!("Destructor: {}", self.0); }
}
fn without_iife() {
let _m0 = Msg("outside block");
{
let _m1 = Msg("before decl");
let _b = Bees;
let _m2 = Msg("after decl");
}
}
fn with_iife() {
let _m0 = Msg("outside block");
(move || {
let _m1 = Msg("before decl");
let _b = Bees;
let _m2 = Msg("after decl");
})()
}
pub fn main() {
println!("Without IIFE:");
let _ = thread::spawn(move || without_iife()).join().unwrap_err();
println!("");
println!("With IIFE:");
let _ = thread::spawn(move || with_iife()).join().unwrap_err();
}
// Without IIFE:
// Destructor: after decl
// thread '<unnamed>' panicked at 'Not the bees!', src/iife-thing.rs:5
//
// With IIFE:
// Destructor: after decl
// thread '<unnamed>' panicked at 'Not the bees!', src/iife-thing.rs:5
// Destructor: outside block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment