4 ms·
> This is one of the few things the C++ type system almost gets right. You really can say for your C++ type no, it doesn't have a default. Rust does even bette
by ekidd 2y ago
> This is one of the few things the C++ type system almost gets right. You really can say for your C++ type no, it doesn't have a default.
Rust does even better here, I think, for fairly subtle reasons:
- Exceptions are replaced by Result, with explicitly-marked return points. They're far less magic. ("panic!" is still magic, but it's allowed to be a call to "abort", so you only use it when the world is burning.)
- "Move semantics" by default makes a lot of edge cases cleaner. If you move out of an object by default, it gets consumed, not set to a default value. Similarly, moving values into a newly-constructed object can't fail, because it's just memmove.
- Object construction is atomic from the programmer's perspective.
- Default initialization only works if you implement "Default". Which is actually just a normal trait with no magic.
- If you discover that you have a state machine, you can switch to discriminated unions ("enum") to make your states mutually exclusive and clearly represented.
This whole area is one of the better parts of Rust's design.
- kelnos 2y ago> Default initialization only works if you implement "Default". And -- and I think this is important -- even then, you have to explicitly assign the default value[0]. You won't just somehow magically get a default value. [0] E.g.: let foo: SomeType = Default::default();