Files
Rust-High-Performance/Chapter10/example5.rs
2018-03-28 11:19:46 +05:30

44 lines
991 B
Rust

use std::thread;
struct MyStruct {
name: String,
}
impl Drop for MyStruct {
fn drop(&mut self) {
if thread::panicking() {
println!("The thread is panicking with the {} struct!", self.name);
} else {
println!("The {} struct is out of scope :(", self.name);
}
}
}
fn main() {
let my_struct = MyStruct {
name: "whole program".to_owned(),
};
{
let scoped_struct = MyStruct {
name: "scoped".to_owned(),
};
}
let handle = thread::Builder::new()
.name("bad thread".to_owned())
.spawn(|| {
let thread_struct = MyStruct {
name: "thread".to_owned(),
};
panic!("Panicking inside the thread!");
})
.expect("could not create the thread");
println!("After thread spawn!");
if handle.join().is_err() {
println!("Something bad happened :(");
}
println!("After everything!");
}