misc/process/wait.html)
    9. [ **20.6.** Filesystem Operations ](../std_misc/fs.html)
    10. [ **20.7.** Program arguments ](../std_misc/arg.html) â± 
    11.       1. [ **20.7.1.** Argument parsing ](../std_misc/arg/matching.html)
    12. [ **20.8.** Foreign Function Interface ](../std_misc/ffi.html)
  41. [ **21.** Testing ](../testing.html) â± 
  42.     1. [ **21.1.** Unit testing ](../testing/unit_testing.html)
    2. [ **21.2.** Documentation testing ](../testing/doc_testing.html)
    3. [ **21.3.** Integration testing ](../testing/integration_testing.html)
    4. [ **21.4.** Dev-dependencies ](../testing/dev_dependencies.html)
  43. [ **22.** Unsafe Operations ](../unsafe.html) â± 
  44.     1. [ **22.1.** Inline assembly ](../unsafe/asm.html)
  45. [ **23.** Compatibility ](../compatibility.html) â± 
  46.     1. [ **23.1.** Raw identifiers ](../compatibility/raw_identifiers.html)
  47. [ **24.** Meta ](../meta.html) â± 
  48.     1. [ **24.1.** Documentation ](../meta/doc.html)
    2. [ **24.2.** Playground ](../meta/playground.html)

__ __

  * Light (default) 
  * Rust 
  * Coal 
  * Navy 
  * Ayu 

__

#  Rust By Example

[ __ ](../print.html "Print this book") [ __ ](https://github.com/rust-
lang/rust-by-example "Git repository") [ __ ](https://github.com/rust-
lang/rust-by-example/edit/master/src/trait/drop.md "Suggest an edit")

#  Drop

The [ ` Drop ` ](https://doc.rust-lang.org/std/ops/trait.Drop.html) trait only
has one method: ` drop ` , which is called automatically when an object goes
out of scope. The main use of the ` Drop ` trait is to free the resources that
the implementor instance owns.

` Box ` , ` Vec ` , ` String ` , ` File ` , and ` Process ` are some examples
of types that implement the ` Drop ` trait to free resources. The ` Drop `
trait can also be manually implemented for any custom data type.

The following example adds a print to console to the ` drop ` function to
announce when it is called.

    
    
    struct Droppable {
        name: &'static str,
    }
    
    // This trivial implementation of `drop` adds a print to console.
    impl Drop for Droppable {
        fn drop(&mut self) {
            println!("> Dropping {}", self.name);
        }
    }
    
    fn main() {
        let _a = Droppable { name: "a" };
    
        // block A
        {
            let _b = Droppable { name: "b" };
    
            // block B
            {
                let _c = Droppable { name: "c" };
                let _d = Droppable { name: "d" };
    
                println!("Exiting block B");
            }
            println!("Just exited block B");
    
            println!("Exiting block A");
        }
        println!("Just exited block A");
    
        // Variable can be manually dropped using the `drop` function
        drop(_a);
        // TODO ^ Try commenting this line
    
        println!("end of the main function");
    
        // `_a` *won't* be `drop`ed again here, because it already has been
        // (manually) `drop`ed
    }
    

[ __ ](../trait/ops.html "Previous chapter") [ __ ](../trait/iter.html "Next
chapter")

[ __ ](../trait/ops.html "Previous chapter") [ __ ](../trait/iter.html "Next
chapter")