5 ms·
So the equivalent of std::move will also make sure to "null" the original object at the same time?
by shortrounddev 3y ago
So the equivalent of std::move will also make sure to "null" the original object at the same time?
- VWWHFSfQ 3y agoIt's not just a matter of "null"ing the reference. The program just won't compile at all if you try to use that reference again
- dataflow 3y agoSurely there's more to it than this? Otherwise you can just set up bugprone-use-after-move checks in C++ and call it a day.
- dmkolobov 3y agoThe difference is that your C++ checks are executed at runtime. Rust's borrow-checker rules ensure this occurs at compile-time, before you ever run any code.
- dataflow 3y agoThe check I mentioned is a compile time check. I'm not referring to ASAN.
- masklinn 3y agoWell yes for starters this is not an optional check and it’s not on just “bug-prone” moves it’s on all for them. Then it also has a borrow checker so if you refer to the content of an other object the langage ensures the (non-owning) pointer does not outlive the pointee, at compile-time. Though this concept is lexical so it’s quite restrictive. And then it encodes some forms of thread-safety in the langage directly. It also removes things like nullable pointers.
- duped 3y agoThe "more to it" is that the use-after-move checks aren't bugprone in Rust, you cannot use a value after it has been moved in safe code (additionally, when you call a function, values are moved and not copied, and when you capture a value in a closure, it is either by unique mutable reference, immutable reference, or moved and therefore safe*). Essentially all the stuff that makes std::move questionable "just works" in Rust. It doesn't even exist, values are moved by default and clones must be explicit (equivalent of a copy constructor for non-trivially copyable types). The other giant advantage is that use-after-free doesn't exist in safe Rust, since you can't have a reference to a value outlive the value. * It is also a compile error for a closure that captures a value by reference to outlive the value.
- dataflow 3y ago> the use-after-move checks aren't bugprone in Rust I can't tell if one if us is confused here or if this is just confusing wording, but the check I referred to is not bug-prone. The pattern of using something after it's been moved from is bug-prone (even though it'd work fine for things like integers, in either language), and it's checking for that bug-prone pattern, hence the name.
- db48x 3y agoIn Rust it’s not bug–prone. The compiler knows which types are moved and which are copied, and if you write code that uses something after it was moved you get a compile–time error.
- _flux 3y agoIn practical terms the original variable or field will be out of scope after the move (well not really, but that's how effective the checking is).