Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot print color escape codes to the terminal

When I run this script:

fn main() {
    // \033[0;31m <- Red
    // \033[0m <- No Color
    println!("\033[0;31mSO\033[0m")
}

I expect to get

SO #in red letters

However, I get:

33[0;31mSO33[0m

When I ran similar script in Go or Python, I get the expected output. What is going on? What am I missing? How do fix this?

I am using:

$ rustc --version
rustc 1.3.0 (9a92aaf19 2015-09-15)
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty
like image 201
Akavall Avatar asked Sep 05 '25 03:09

Akavall


1 Answers

Rust 1.3.0 does not seem to support octal escape strings such as \033. Instead, you can use hexadecimal escape strings like \x1b.

fn main(){
  println!("\x1b[0;31mSO\x1b[0m")
}

Updated: This answer was intended that "you cannot use octal character such as \033, use hexadecimal character \x1b instead", but If you want to know about ANSI Escape code, maybe this gist is useful.

like image 137
ymonad Avatar answered Sep 07 '25 19:09

ymonad