Being a novice C++ programmer learning Rust, I am not fully comfortable with ownership and how the borrow checker works, so while experimenting with it, I've come to an analogy that I wanted to inquire about. Could I think of rusts' transfer of ownership as the compiler inserting a free immediately after any function call that borrows any heap-allocated value, as an example:
analogy(Type* val) {
...
}
function() {
Type* arg = new Type()
analogy(arg)
// Could I think of the borrow checker on assumption
// as a preprocessor that inserts the following
delete arg
// After doing so, a static analyzer is run by the compiler
// which can trivially detect an error such as this
print(arg)
}
The following explanation is a simplified model but it should get you started.
newanddeleteare opposites, just like{and}are. It's best to think about it with lots of braces where the opening brace resembles the introduction of a fresh variable and possiblynew. The closing brace represents the end of the variable's life time and possiblydelete: