RAII

Variables in Rust do more than just hold data in the stack: they also own
resources, e.g. Box<T> owns memory in the heap. Rust enforces RAII
(Resource Acquisition Is Initialization), so whenever an object goes out of
scope, its destructor is called and its owned resources are freed.

This behavior shields against resource leak bugs, so you’ll never have to
manually free memory or worry about memory leaks again! Here’s a quick showcase:

  1. // raii.rs
  2. fn create_box() {
  3. // Allocate an integer on the heap
  4. let _box1 = Box::new(3i32);
  5. // `_box1` is destroyed here, and memory gets freed
  6. }
  7. fn main() {
  8. // Allocate an integer on the heap
  9. let _box2 = Box::new(5i32);
  10. // A nested scope:
  11. {
  12. // Allocate an integer on the heap
  13. let _box3 = Box::new(4i32);
  14. // `_box3` is destroyed here, and memory gets freed
  15. }
  16. // Creating lots of boxes just for fun
  17. // There's no need to manually free memory!
  18. for _ in 0u32..1_000 {
  19. create_box();
  20. }
  21. // `_box2` is destroyed here, and memory gets freed
  22. }

Of course, we can double check for memory errors using valgrind:

  1. $ rustc raii.rs && valgrind ./raii
  2. ==26873== Memcheck, a memory error detector
  3. ==26873== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
  4. ==26873== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
  5. ==26873== Command: ./raii
  6. ==26873==
  7. ==26873==
  8. ==26873== HEAP SUMMARY:
  9. ==26873== in use at exit: 0 bytes in 0 blocks
  10. ==26873== total heap usage: 1,013 allocs, 1,013 frees, 8,696 bytes allocated
  11. ==26873==
  12. ==26873== All heap blocks were freed -- no leaks are possible
  13. ==26873==
  14. ==26873== For counts of detected and suppressed errors, rerun with: -v
  15. ==26873== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

No leaks here!

Destructor

The notion of a destructor in Rust is provided through the Drop trait. The
destructor is called when the resource goes out of scope. This trait is not
required to be implemented for every type, only implement it for your type if
you require its own destructor logic.

Run the below example to see how the Drop trait works. When the variable in
the main function goes out of scope the custom destructor will be invoked.

  1. struct ToDrop;
  2. impl Drop for ToDrop {
  3. fn drop(&mut self) {
  4. println!("ToDrop is being dropped");
  5. }
  6. }
  7. fn main() {
  8. let x = ToDrop;
  9. println!("Made a ToDrop!");
  10. }

See also:

Box