41

How do I output colored text to the terminal using Rust? I've tried using the special escape characters that I found in this python answer, but they just print literally. Here is my code:

fn main() {
    println!("\033[93mError\033[0m");
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • 1
    This seems to be a duplicate of https://stackoverflow.com/q/33139248 . – Garret Wilson Mar 16 '23 at 13:24
  • Good point! I certainly hadn't found that question in my pre-post search. Does that mean we should close this question? – Sylvester Kruin Mar 16 '23 at 15:19
  • 1
    Yeah, in a perfect world the later question should probably be closed with a link that it is duplicating the original. But I'm new to Rust and haven't read these posts super closely, so I left it as it is and just noted the duplication. – Garret Wilson Mar 16 '23 at 15:28

3 Answers3

66

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 its 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.

Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • 3
    Another crate to check is https://lib.rs/crates/termcolor and its wrapper https://lib.rs/crates/termcolor_output (disclaimer - I've created the latter, but not the former). – Cerberus Nov 16 '21 at 03:04
  • 1
    @Cerberus I encourage you to post another answer, if you like! The more the merrier ;-)! – Sylvester Kruin Nov 16 '21 at 16:05
  • 4
    Also I don't think you need to use the `format!` macro every time - you can just call the `Colorize` functions on the actual `&str` (e.g. `"Bold".bold()` would work instead of `format!("Bold").bold()`) – iamkneel Oct 08 '22 at 05:24
48

Rust doesn't have octal escape sequences. You have to use hexadecimal:

println!("\x1b[93mError\x1b[0m");

See also https://github.com/rust-lang/rust/issues/30491.

What happens, and the reason the compiler does not complain, is that \0 is a valid escape sequence in Rust - and represents the NULL character (ASCII code 0). It is just that Rust, unlike C (and Python), does not allow you to specify octal number after that. So it considers the 33 to be normal characters to print.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
1

This works for me:

cargo add inline_colorization

and in main.rs:

use inline_colorization::*;

fn main() {
  println!("Lets the user {color_red}colorize{color_reset} the and {style_underline}style the output{style_reset} text using inline variables");
}

I am the creator of the mentioned rust crate.