Q:

What is the equivalent of Java's transient in Serde?

I am interested in making a variable not automatically serialized (e.g., by Serde) with the use of a keyword like Java's transient, but in Rust. I need this to store passwords. Of course, I could manually clear the data upon serialization to the disk, but I would like to know if there are better, more automatic, options.

A:

You can use the #[serde(skip)] attribute:
use serde::{Deserialize, Serialize}; // 1.0.88

#[derive(Deserialize, Serialize)]
struct MyStruct {
    field1: i32, // this will be (de)serialized
    #[serde(skip)]
    field2: i32, // this will be skipped
}

If the type needs to be deserialized, it's a good idea to accompany a #[serde(skip)] with a #[serde(default)] (or a #[serde(default = "fn_name")]) attribute. Serde deserializes  skipped fields as if a #[serde(default)] was implicitly added, but it's clearer to someone reading your code if you make it explicit where this value will come from.
See:

List of all serde field attributes
All serde attributes

