Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare an Enum inline with a struct declaration

Tags:

syntax

rust

I could have a structure like the following:

struct Example {
    state: State,
    properties: HashMap<String, String>,
}

enum State {
    a, b, c,
}

If the Example structure is the only user of the State enum, I think it would make sense to declare it in the struct:

struct Example {
    state: enum { a, b, c },
    properties: HashMap<String, String>,
}

Is there any valid syntax for this?


1 Answers

No. There is no such syntax.

You can check the Rust grammar for structs, the type of a field must be a type expression. Type expressions cannot create new types.

like image 83
mcarton Avatar answered Oct 30 '25 07:10

mcarton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!