▲ | john-h-k 4 days ago | |||||||||||||||||||||||||||||||||||||||||||
> There is one owner, and the owner can not free the memory as long as there is a borrower (it doesn't matter whether the borrower is mutable or not). So that's the task of the borrow checker. Yes, but what about: ``` let mut x = &mut some_vec; some_vec.push(10); ``` Sure the vec has an owner, but the second mutable borrow causes it to invalidate the first pointer. You need a _third_ category, “unstable mut”, which is exclusive as it can cause an object’s internal data to move. You can then collapse mut and immutable to one… but you end up with the exact some colouring problem between unstable mut and the others | ||||||||||||||||||||||||||||||||||||||||||||
▲ | thomasmg 4 days ago | parent [-] | |||||||||||||||||||||||||||||||||||||||||||
Yes, I see your point. In Rust, the owner, and the mutable borrower can change the pointer itself (like C realloc). If multiple "mut" borrows are allowed, then this would be unsafe, so I understand "unstable mut" would solve this problem - but result in a new colouring problem. My solution to this would be: in a new language, do not allow reallocation. Not for owners, and not for mut borrow. This is what Java does: an ArrayList is a wrapper around an array, and so adding entries will not move the ArrayList object, just the (wrapped) private array. In Java the programmer never sees dangling references. Java prevents the issue by design, at the cost of always paying for the wrapper. If you want to avoid this price, you need to use the array directly. (I have to admit I was not aware that in Rust, push moves the memory... so thanks a lot for explaining! Always learning something new.) I don’t have any problem with Rust’s focus on performance. But its design does make life harder for developers than it needs to be. It doesn't match my vision of a programming language that is at the same time easy to use, safe, and nearly as fast as C. | ||||||||||||||||||||||||||||||||||||||||||||
|