expression
Untagged enums and ambiguity
Deserialize by attempting variants in declaration order and selecting the first successful match. Avoid ambiguous shapes; a successful parse may not identify the sender’s intent. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): count 8 label eight Common mistake: Using overlapping variants without defining which interpretation takes precedence.
Formal Syntax & Structure
rust syntaxrust
#[serde(untagged)]Working Example
Example Snippetrust
fn main() { #[derive(serde::Deserialize)] #[serde(untagged)] enum Input { Count(u32), Label(String) } for text in ["8", r#""eight""#] { match serde_json::from_str::<Input>(text).unwrap() { Input::Count(n) => println!("count {n}"), Input::Label(s) => println!("label {s}"), } }}