Created
December 23, 2015 18:20
-
-
Save jld/e96702a18478a1c8aa8c to your computer and use it in GitHub Desktop.
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
| 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