How do I print colored text to the terminal in Rust?

You can use the colored crate to do this. Here is a simple example. with multiple colors and formats:

use colored::Colorize;

fn main() {
    println!(
        "{}, {}, {}, {}, {}, {}, and some normal text.",
        "Bold".bold(),
        "Red".red(),
        "Yellow".yellow(),
        "Green Strikethrough".green().strikethrough(),
        "Blue Underline".blue().underline(),
        "Purple Italics".purple().italic()
    );
}

Sample color output:

colored and formatted text in the terminal with Rust

Each of the format functions (red(), italics(), etc.) can be used on it’s own as well as in combination with others. But if you use colors in combination with each other, only the last color to be set shows.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)