Little confusion with "[serde(rename)]'"

Why can [serde::rename] specify deserialize? In my case, I need the following to replace hyphens by underscores:

#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectSettings {
    #[serde(rename = "short-id")]
    pub short_id: String,
    #[serde(rename = "full-id")]
    pub full_id: String,

I've not tested it yet (this will be my first time manipulating Toml), but I'm finding serde's documentation a bit vague on this. Isn't only serialize name needed for [serde(rename)]? Field attributes · Serde What does deserialize do for [serde(rename)]?

This is mostly a matter of consistency, allowing you to separately configure the rename used by #[derive(Serialize)] and #[derive(Deserialize)] if you so wish. It is true that this isn't actually all that useful in practice, but this results simply from the uniform way of making any serde attributes only apply to one or the other derive instead of both.

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.