expression

Externally tagged enums

The default representation puts a variant name outside its associated content. A unit variant typically becomes a JSON string. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): "Ready" {"Count":4} Common mistake: Expecting all enum representations to use an identical JSON shape.

Formal Syntax & Structure

rust syntaxrust
#[derive(serde::Serialize, serde::Deserialize)] enum Event { ... }

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Serialize)]    enum Event { Ready, Count(u32) }    println!("{}", serde_json::to_string(&Event::Ready).unwrap());    println!("{}", serde_json::to_string(&Event::Count(4)).unwrap());}
Rust Reference