| ▲ | clownstrikelol 3 hours ago | |
Sure you can, bringing data along for the ride is optional in Rust. Nothing is forcing you to use it like a tagged union, it’s an optional (but useful) feature. If you want specific values (integer or otherwise) associated with specific variants (i.e. Human = 1, Cat = 4) you can do that too. Use the #[repr(T)] or impl the From / Into traits yourself. | ||
| ▲ | xeubie 3 hours ago | parent [-] | |
No, you can't, because you're misunderstanding my point. You can make an enum in rust that doesn't contain data, but you can't make an enum that is explicitly backed by a tag type. `#[repr(u8)]` lets the tag be a u8 with values like Human = 1, Cat = 4, but you can’t say "Person's tag is my PersonKind enum." Zig lets you do exactly that with `union(PersonKind)`. In Rust, PersonKind would always be a separate type you keep in sync by hand. | ||