Hacker News new | past | comments | ask | show | jobs | submit login

If you are very zealous and use lots of C++17/20 magic, you can prevent lots of runtime bugs with C++ too by doing basically the same stuff you do in Rust (RAII, move semantics, use concepts, etc). Sadly that's not doable in C#.



Move in particular is painful in C++ because to make it work C++ needs to invent these "hollowed out" objects that would be safe to deallocate after there's no longer a real value inside them. Rust doesn't need to do that.

Suppose you've got a local String variable A. You will move A into a data structure which is going to live much longer and then your local function exits.

In C++ when the function exits, A will get destroyed, so when A is moved into the data structure, A needs to be hollowed out so that whatever is left (a String object with no heap storage) can be safely destroyed on function exit.

In Rust the compiler knows you moved A, therefore there is nothing to destroy at the end of the function, no work needed (at runtime).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: