3 ms·
Sorry it's probably just me but there is nothing syntactically obvious about ownership to me in the example you gave other than the names you gave things.
by Guthur 2y ago
Sorry it's probably just me but there is nothing syntactically obvious about ownership to me in the example you gave other than the names you gave things.
- j-pb 2y agoIt all boils down to forbidding this: let x = 5 do_something_with(x) do_something_with(x) // <- boom compiler error you simply cannot use the same variable twice, because you lost ownership of the value it stores on the first call. But that's a bit cumbersome, so people add an operator that allows you to derive a new value from a variable without taking the old one. Let's call that one `&`. It takes a variable that would normally only have been allowed to be used once (because using it removes the value from it), and returns a "thing" that borrows the contents of the variable out, and behaves a lot like the original thing, but not quite. let x = 5 do_something_with(&x) do_something_with(&x) And because those derived things can be tracked and only used once just like the original object we can make sure that no two things can derive such borrowed things at the same time in a way that is mutating the original thing. Let's call the operator that only allows one mutable borrow to exist at a time `&mut`. The ability to track that only one of these can exist at any given moment for any value is based not on some special properties of "borrows" or "references", but because of the ability to uniquely track ANY value.