Being able to say 'Rust enums can carry arguments' - for some reason - sounds less intimidating and conveys the core feature.
(but yeah, 'enum with arguments' also describes it very well)
But honestly I think enum was a better choice.
Also, Rust does have actual tagged unions for C interop that you have to define yourself as a `struct` with an int field and a `union` field, just like in C.
Their representation in memory may not use it, but it's still defined.
There are also zero variants enums that don't have any discriminant, but still could be used.
I'm not sure what's the point of comparing it to C because enums in C only carry tags and no data, while unions only carry data and no tag. Enums in rust could do both.
Combine it with switches and you get compiler guarantees that every state is explicitly handled, and if you are in a particular state you always have the relevant child objects.
I remember being shocked dart lacked this functionality when I tried out flutter.
They aren't as ergonomic or type-safe, and rather surprisingly the match statement is not an expression in Python's grammar, but regardless of its problems the match statement is very powerful, even more so than static equivalents.
[1] https://docs.python.org/3/library/stdtypes.html#types-union
[2] https://mypy.readthedocs.io/en/stable/type_narrowing.html
[3] https://docs.python.org/3/library/typing.html#typing.assert_...
I had to try:
pub enum Foo {
Foo { a: i32 },
}
impl Foo {
pub fn new() -> Self {
Foo::Foo { a: 42 }
}
pub fn get_a(Foo::Foo{a}: &Self) -> &i32 {
a
}
}
At opt level above zero (-C opt-level=1) the tag is elided: example::Foo::new:
mov eax, 42
ret
example::Foo::get_a:
mov rax, rdi
ret
https://godbolt.org/z/qKzMqvhb7