Anonymous/struct-local enums

May be it's in the language, but I wasn't able to find it. Are there an ability to create per-structure enums?

(This code does not compile).

pub struct Value{
    x: f64,
    y: f64,
    type: enum {
        Exact,
        Positive,
        Negative,
        Out_of_Domain
    }
}```

This enum is never used outside of the structure, so it would be nice to have it declared inside structure.

Is there a way to do something like this?

This feature doesn't exist. The enum can easily be declared as a private enum next to the struct - and then you can add the required derives/trait impls it too, once the type has a name (you'll find that this is close to mandatory, let's say if you for example want the enum values to be Copy).

3 Likes