"Is there a way to iterate over enum values in Rust like in Java"

"No, because they are more powerful"


  • 11
    I feel I need coffee.
    Enums aren't enums anymore?
  • 5
    @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
  • 4
    @12bitfloat ok me be confused.
    *looks up docs*

    So no, enums not be enums but a collection of what ever you wants.
  • 8
    And that kids is how you take something out of context to make it funny.
  • 2
    @trickory To be fair I haven't taken anything out of context 🤷
  • 2
    So Rust enums are structs? And you are using structs to fake enums?
  • 6
    @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
  • 3
    I am going to opt for the blue pill at this point in time.
  • 1
    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.
  • 1
    Rust is homosexual
  • 2
    @irene It's only gay in so far as to fuck me in the ass constantly
  • 0
    The language is named as Rust. It only makes sense that it is so rusty! Don't use it.
  • 1
    I 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.
  • 0
    @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
  • 1
    @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.
  • 2
    Also if your think Rust should have named enums differently, i think we should call it "enums++" or "enums with classes"
  • 0
    Can you do the following with a Rust enum? I haven't found a way
  • 0
  • 0
    @highlight I think there is a crate to do something like that. But not sure what it was called.
  • 0
  • 0
    So Rust enums are just tagged unions apparently
Add Comment