[Serde-rs] How to deserialize Vec<MyStruct>?

I would like deserialize a yaml to the Vec.

#[derive(Debug, Deserialize)]
struct Data {
    #[serde(deserialize_with = "deserialize_enum")]
    fields: Vec<EnumValue>,

#[derive(Debug, Serialize, Deserialize, Eq)]
struct EnumValue
    id: String,
    value: u8, // enum value: 0..255
    description: String,


      - id: FAKE
        value: 0
        description: Reserve;


You don't need to do anything special


The standard derive will correctly deserialize your data

1 Like

Ultimately I want to have an index from Vector in the value field.

      - id: FAKE
        description: foo
      - id: FAKE2
        description: bar 
Data { fields: [
     EnumValue { id: "FAKE", value: 0, description: "foo" },
     EnumValue { id: "FAKE2", value: 1, description: "bar" },
 ] }

So something like this? Rust Playground

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.