Definition of a struct with a field name "use" error

Viewed 38

I'm trying to Deserialize a json response into a struct

#[derive(Deserialize, Debug)]
struct Jwks {
    alg: String,
    kty: String,
    use: String,
    n: String,
    e: String,
    kid: String,
    x5t: String,
    x5c: String
}

But it seems that the use keyword is reserved and cannot be used as a struct field name. Is there a way to fix this?

1 Answers

You can specify a custom name with rename. Like this:

#[serde(rename = "use")]
s_use: String,

Here is a complete example with your struct:

#[derive(Deserialize, Debug)]
struct Jwks {
    alg: String,
    kty: String,
    #[serde(rename = "use")]
    s_use: String,
    n: String,
    e: String,
    kid: String,
    x5t: String,
    x5c: String
}
Related