Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@C0D4 This really annoys me about Rust. Enums are algebraic data types, not enums. Right now I'm trying to assign data to enums which doesn't work so I have to fake enums by actually using structs and constants. Fun
-
C0D4681385y@12bitfloat ok me be confused.
*looks up docs*
So no, enums not be enums but a collection of what ever you wants. -
Lythenas2455y@Demolishun No. Rust enums are just ADTs. Meaning a variant of the enum can be a struct and contain data.
In contrast enums in Java are fancy ints -
In Swift, enums CAN have associated values, where it makes no sense to iterate them.
But normal enums without associated values can be iterated by the synthesized static "cases" property, which contains all enum values in an array.
I don't know rust but I would bet that something similar applies to rust, too. -
Wisward235yI guess nobody understands rust than. These enums are a fundamental part of SAFETY in the language. Result<> could return an OK or Error that contains the error message.
-
@Wisward I just wish they either weren't called enums, which they aren't, or we would have the ability to assign associated members, sortof how Java allows making each enum value into an anonymous class
-
Wisward235y@12bitfloat Rust enums can be used exactly like Java's enums. If you don't want data in your enums, don't put data in to them. If you want to see use cases, see the chapter 6-1 of the Rust book. It gives an example of how Rust enums shorten and simplify your code.
-
Wisward235yAlso if your think Rust should have named enums differently, i think we should call it "enums++" or "enums with classes"
-
Lythenas2455y@highlight I think there is a crate to do something like that. But not sure what it was called.
-
So Rust enums are just tagged unions apparently
https://doc.rust-lang.org/edition-g...
Related Rants
"Is there a way to iterate over enum values in Rust like in Java"
"No, because they are more powerful"
🤔🤔🤔
rant
wtaf
rust