PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

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

 August 14, 2022     output, rust, terminal     No comments   

Issue

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");
}

Any insight is welcome!


Solution

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.

Edit: 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.



Answered By - Chayim Friedman
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing