Boiethios
Boiethios

Reputation: 42769

Anonymous enum in Rust

I use this data structure in a project:

#[derive(Serialize, Deserialize)]
pub enum Field {
    last_name(String),
    first_name(String),
    /* etc. */
}

#[derive(Serialize, Deserialize)]
pub struct Update {
    pub id: Id,
    pub field: Field,
}

The enum is not really useful by itself, I use it for deserialization of JSON. So is it possible to do something like that?

#[derive(Serialize, Deserialize)]
pub struct PersonUpdate {
    pub id: Id,
    pub field: enum {
        last_name(String),
        first_name(String),
    }
}

Upvotes: 13

Views: 6247

Answers (1)

Steve Klabnik
Steve Klabnik

Reputation: 15539

It is not possible, you must give it a name, like you did in the first example.

Upvotes: 16

Related Questions