#6471·clap

Error::render() doc example teaches a color-stripping path — println!("{err}") is always plain

Author: SeekuhCrewCreated Aug 8, 2026Updated Sep 22, 2026
LabelsC-enhancementA-docsS-waiting-on-design

Please complete the following tasks

Rust Version

rustc 1.99.0-nightly (504869653 2026-08-03)

Clap Version

4.6.6

Also reproduced on current master (clap_builder/src/error/mod.rs, clap_builder/src/builder/styled_str.rs).

Minimal reproducible code

rust
use clap::{Arg, Command};

fn main() {
    let err = Command::new("t")
        .arg(Arg::new("x").long("x").required(true))
        .try_get_matches_from(["t"])
        .unwrap_err();

    err.print().unwrap();           // colored on a terminal (stderr)
    eprintln!("{}", err.render());  // always plain, even on a terminal (stderr)
}

Steps to reproduce the bug with the above code

cargo run

Run it in a terminal (not piped), and compare the two lines on stderr. print() emits ANSI-colored output; the render() + Display path — the one shown in render()'s own doc example — emits plain text.

Actual Behaviour

Error::render() returns a StyledStr whose internal string carries the ANSI codes, but StyledStr::Display is documented as "Color-unaware printing. Never uses coloring" and strips them unconditionally (styled_str.rs: iter_text → anstream::adapter::strip_str). Error::print() renders the same data through a Colorizer + anstream::AutoStream, which is TTY-aware. Same data, two public paths, different output — and render()'s doc example teaches exactly the degraded path:

rust
let err = err.render();
println!("{err}"); // always plain, even on a TTY

Expected Behaviour

The doc example should either use .render().ansi() or explicitly note that Display strips styling, so the documented path does not silently degrade the output the method just produced.

Additional Context

Impact: any caller that captures the error as a string — tests asserting on output, logging, forwarding to a file or another process — silently loses styling, and the documented path actively teaches this. Working around it today means re-implementing the Colorizer/AutoStream TTY detection externally.

Debug Output

N/A.