expression
Serde default: missing fields
Fill an omitted field using Default or a named function. A field containing an invalid type is not the same as an omitted field. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): 3 true Common mistake: Expecting default to fix a wrong or null value for a nonoptional field.
Formal Syntax & Structure
rust syntaxrust
#[serde(default)] / #[serde(default = "function")]Working Example
Example Snippetrust
fn main() { fn retries() -> u8 { 3 } #[derive(serde::Deserialize)] struct Settings { #[serde(default = "retries")] retries: u8 } let s: Settings = serde_json::from_str("{}").unwrap(); println!("{}", s.retries); println!("{}", serde_json::from_str::<Settings>(r#"{"retries":null}"#).is_err());}