expression
Serde flatten: combine named fields
Embed the serialized fields of a nested struct or map in its parent object. Avoid conflicting keys and do not combine with deny_unknown_fields. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): Baku true Common mistake: Expecting a nested address key to remain after flattening.
Formal Syntax & Structure
rust syntaxrust
#[serde(flatten)]Working Example
Example Snippetrust
fn main() { #[derive(serde::Serialize)] struct Address { city: String } #[derive(serde::Serialize)] struct User { id: u32, #[serde(flatten)] address: Address } let v = serde_json::to_value(User { id: 3, address: Address { city: "Baku".into() } }).unwrap(); println!("{} {}", v["city"].as_str().unwrap(), v.get("address").is_none());}