Advertisement
Guest User

finalizer in Rust

a guest
Jan 7th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.83 KB | None | 0 0
  1. > Same in Rust. For example, a Box or Vec owns memory on the heap - if they don’t dealloc it in drop, it’s leaked. There’s no real difference between C++ and Rust here.
  2.  
  3. Try to run the following code. The drop() function of the ObjectA doesn't destruct the 'b' member, i.e. it doesn't call to any equivalent of the delete operator of C++. But the it doesn't leak.
  4.  
  5.    struct ObjectA {
  6.        b: Box<ObjectB>
  7.    }
  8.    
  9.    struct ObjectB;
  10.    
  11.    impl Drop for ObjectA {
  12.        fn drop(&mut self) {
  13.            println!("dropping ObjectA")
  14.        }
  15.    }
  16.    
  17.    impl Drop for ObjectB {
  18.        fn drop(&mut self) {
  19.            println!("dropping ObjectB")
  20.        }
  21.    }
  22.    
  23.    fn main() {
  24.        {
  25.            let _a: ObjectA = ObjectA { b: Box::new(ObjectB) };
  26.        }
  27.        println!("End")
  28.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement