3 ms·
So everyone else is focusing on the memory allocation side of ownership. The other side to this is aliasing / borrowing / handling references and mutability.
by epage 6y ago
So everyone else is focusing on the memory allocation side of ownership. The other side to this is aliasing / borrowing / handling references and mutability.
In Python, you might have a `list` that you built up and them pass its data to multiple functions. You assume these functions are do not mutate their arguments and you keep passing the same `list` over again. In this case, the caller is controlling the invariants of the `list` and therefore "Owns" it.
Deep down, one of those functions needs to mutate a `list` to do its calculations. Where the mutation is happening, it assumes it is the "Owner" and can modify the `list` without violating any invariants. Unfortunately, a "Borrowed" list was passed in and now the caller's invariants are violated.
Rust's ownership model helps to catch this kind of problem at compile time. By default, everything is single-owner though you can change that with a `Mutex`.
- creata 6y agoYep. Even when you're doing something as simple as iterating over some data structure, you have to remember not to mutate it. Rust enforces that, but Python just either corrupts the data structure / iterator, or if you're lucky it'll throw an exception.