4 ms·
That's a gross misunderstanding of `unsafe`'s place in Rust, and also of the use cases for `union` vs `enum`.
by Rusky 4y ago
That's a gross misunderstanding of `unsafe`'s place in Rust, and also of the use cases for `union` vs `enum`.
- Kukumber 4y agoI'm not well versed in Rust, care to educate me to clear misunderstanding? What's the Rust translation of this example?: https://ziglang.org/documentation/master/#Tagged-union https://ziglang.org/documentation/master/#Tagged-union
- TakeBlaster16 4y agoenum MyEnum { Ok(u8), NotOk } fn main() { let value = MyEnum::Ok(42); assert!(matches!(value, MyEnum::Ok(_))); match value { MyEnum::Ok(x) => assert_eq!(x, 42), MyEnum::NotOk => unreachable!(), } } No unsafe to be found. The feature is there, it just has a different keyword than you might be used to -- the same way Haskell and Java both have something called "class" that mean different things.
- Cyph0n 4y agoRust enums are a form of tagged unions. Rust unions are plain old unions that are mainly used for C interop. This is why using them requires unsafe code.