I am pretty certain that there is a dedicated name for this but I have no idea what it is.
When you have a pointer that stops pointing to a valid object, that's a dangling pointer, but what about an object that has no references to it, particularly in Rust?
Take the following code for example:
{
let mut v: Vec<u32> = vec![1, 2, 3];
v = Vec::new();
v.push(0);
}
When v
is reassigned to a whole new vector, what happens to the old one? In C/C++ that is the birth of a memory leak since nobody is gonna free that memory and you no longer have a way to do so either. However, in Rust, there's all kinds of magic happening when exiting a scope (hence the {}
in the sample code.
From a logical point of view, since Rust has no GC, that would dictate that the vector just stays in memory until the process terminates, scanning for unreachable objects when going out of scope would tread on actual GC but I don't yet know enough about Rust internals to make those kinds of guesses (though I'd like to at some point).
What exactly happens in the above code? Is that a memory leak that you have to watch out for just like in C/C++?