January 2018
Beginner to intermediate
454 pages
10h 8m
English
In Rust, there's no garbage collector to deallocate the memory when it's not needed anymore. Also, there's no need for the programmer to specify where the memory should be deallocated. But how can this work? The compiler is able to determine when to deallocate the memory thanks to the concept of ownership; only one variable can own a value. By this simple rule, the matter of when to deallocate the value is simple: when the owner goes out of scope, the value is deallocated.
Let's see an example of how deallocation is related to scope:
let mut vec = vec!["string".to_string()]; if !vec.is_empty() { let element = vec.remove(0); // element is deallocated at the end of this scope. }
Here, we remove an element from the vector in a new ...