Crates often expose feature flags that choose one behavior while keeping the public call site small.

Program

Play the program to choose a JSON-shaped output or a compact text output.

json_output
feature_toggle.rs
Replay: real traced execution (multi-file project)
fn main() {
    let json_output = true;
    let summary = build_summary("search", 3, json_output);
    println!("{summary}");
}

fn build_summary(name: &str, count: i32, json_output: bool) -> String {
    if json_output {
        format!("{{\"name\":\"{name}\",\"count\":{count}}}")
    } else {
        format!("{name}:{count}")
    }
}
fn main() {
    let json_output = false;
    let summary = build_summary("search", 3, json_output);
    println!("{summary}");
}

fn build_summary(name: &str, count: i32, json_output: bool) -> String {
    if json_output {
        format!("{{\"name\":\"{name}\",\"count\":{count}}}")
    } else {
        format!("{name}:{count}")
    }
}
  1. json_output ← true

    1fn main() {2    let json_outpu→ truet = true; //@json_output=true, false3    let summary = build_summary("search", 3, json_outputruet);4    println!("{summary}");
  2. if json_output

    7fn build_summary(name: &str, count: i32, json_output: bool) -> String {8    if json_outputruet {9        format!("{{\"name\":\"{name}\",\"count\":{count}}}")10    } else {11        format!("{name}:{count}")
  3. summary ← "{\"name\":\"search\",\"count\":3}"

    2    let json_output = true; //@json_output=true, false3    let summar→ "{\"name\":\"search\",\"count\":3}"y = build_summary("search", 3, json_outputruet);4    println!("{summary}");5}
    output{"name":"search","count":3}
  1. json_output ← false

    1fn main() {2    let json_outpu→ falset = false;3    let summary = build_summary("search", 3, json_outpufalset);4    println!("{summary}");
  2. summary ← "search:3"

    2    let json_output = false;3    let summar→ "search:3"y = build_summary("search", 3, json_outpufalset);4    println!("{summary}");5}
    outputsearch:3
feature flag `json_output` stands in for a crate feature that selects one output strategy.
branch Only one formatting branch runs for a chosen feature shape.
API boundary `build_summary` hides the formatting choice behind one small function.