January 2019
Beginner to intermediate
554 pages
13h 31m
English
This is the trait we've been referring to quite a few times which does all the magic of automatically freeing up the resources that are used when a value goes out of scope. The Drop trait is akin to what you would call an object destructor method in other languages. It contains a single method, drop, which gets called when the object goes out of scope. The method takes in a &mut self as parameter. Freeing of values with drop is done in last in, first out. That is, whatever was constructed the last, gets destructed first. The following code illustrates this:
// drop.rsstruct Character { name: String,}impl Drop for Character { fn drop(&mut self) { println!("{} went away", self.name) }}fn main() { let steve = Character { name: "Steve".into(), ...Read now
Unlock full access