expression

Skip fields during serialization and deserialization

Exclude a derived field from both wire directions; on reading it gets a default. Use this for derived runtime state, not as an entire secret-handling policy. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): {"name":"index"} 0 Common mistake: Assuming skipped state survives a round trip.

Formal Syntax & Structure

rust syntaxrust
#[serde(skip)]

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Serialize, serde::Deserialize)]    struct Job { name: String, #[serde(skip)] attempts: u32 }    let j = Job { name: "index".into(), attempts: 5 };    let text = serde_json::to_string(&j).unwrap();    let restored: Job = serde_json::from_str(&text).unwrap();    println!("{text}");    println!("{}", restored.attempts);}
Rust Reference