ELI5: Why does rust allow double borrowing?

67 points norlock_dev 26 comments

A bit of a newby question I guess.

I don't understand the idea of having && or &mut &mut, in structs or functions. Shouldn't the Rust compiler be able to calculate whenever something is borrowed and just doesn't free the memory, so It doesn't matter if its borrowed more than once and only allow single borrow for readability?

Somewhere the owner of the memory cleans up the memory and I guess all lifetimes should tickle down. So the borrowed function should extend the lifetime automatically to the last possible lifetime based on the functions it calls?