expression

Separate output format from data selection

Choose human-readable or machine-readable rendering after computing results. The same typed row can be rendered differently. Use explicit format selection and never change machine formats just to decorate a terminal. Fixture: Expected stdout (not executed): {"matched":3} Expected stderr: (empty) Expected exit: 0 Common mistake: Mixing ANSI color or status messages into the selected machine-readable format.

Formal Syntax & Structure

rust syntaxrust
match format { ... }

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Serialize)]    struct Count { matched: usize }    enum Format { Human, Json }    fn render(count: &Count, format: Format) -> String {        match format {            Format::Human => format!("{} matching records", count.matched),            Format::Json => serde_json::to_string(count).unwrap(),        }    }    let count = Count { matched: 3 };    assert_eq!(render(&count, Format::Human), "3 matching records");    println!("{}", render(&count, Format::Json));}
Rust Reference