How to Use Colored Output in Rust CLIs (colored, owo-colors)

Cli
Use the anstyle crate to apply ANSI color styles to Rust CLI output for better readability.

Use the anstyle crate to apply ANSI color styles to your CLI output, matching the approach used in the Rust compiler's highlighter module.

use anstyle::{AnsiColor, Color, Style};

fn main() {
    let style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
    println!("{style}This text is green{style:#}");
}

Add anstyle = "1.0" to your Cargo.toml dependencies.