3 ms·
The assumed fix here is to distinguish between a mutable borrow and the reservation of a location to later do a mutable borrow. Locations based on local variabl
by cwzwarich 10y ago
The assumed fix here is to distinguish between a mutable borrow and the reservation of a location to later do a mutable borrow. Locations based on local variables and their fields can be reserved, although a move out of the variable would invalidate a reservation. Other locations (e.g. the result of a function call) can't be reserved and would give an error.
- eridius 10y agoSeems like it would be confusing to allow let mut nums = Vec::new(); nums.push(nums.len()); but disallow let mut nums = Box::new(Vec::new()); nums.push(nums.len()); The second example would have to be disallowed because the mutable reference is created by calling DerefMut::deref_mut().
- cwzwarich 10y agoThe particular case of Box is safe, and is along the lines of special knowledge about Box that is assumed by the type checker, but the special-cased nature of Box has been gradually decreasing over time. I don't think there would be an easy safe way to allow user-defined types to establish same guarantee. In retrospect, changing the evaluation order of a function with respect to its arguments might have been a better choice, but something like the solution I mentioned is probably the only backwards-compatible option.