drop_guard 0.0.2

The drop_guard crate enables you to implement the Drop trait on any type.
Documentation

drop_guard

crates.io doc.rs Build Status

Use cases

Joining threads when they fall out of scope:

extern crate drop_guard;

use drop_guard::DropGuard;

use std::thread::{spawn, sleep};
use std::time::Duration;

fn main() {
    // The guard must have a name. _ will drop it instantly, which would lead to unexpected results
    let _g = DropGuard::new(spawn(move || {
                            sleep(Duration::from_secs(2));
                            println!("println! from thread");
                        })
                        , |join_handle| join_handle.join().unwrap());
    
    println!("Waiting for thread ...");
}

Examples

Feel free to run the included examples:

cargo run --example rainbow
cargo run --example thread
cargo run --example threadpool