I'm trying to replace all the occurrences of a given string with a colorized version:
extern crate colored; // 1.6.1
use colored::Colorize;
fn print_found_line(x: &i32, line: &String, found: &str) {
let mut line_to_print: String = line.clone();
println!("{}", found.red()); // work
line_to_print = line_to_print.replace(found, found.red().as_ref());
println!("[{}] {}", x.to_string().blue(), line_to_print); // the found string replaced is not red
}
fn main() {}
The first println!
works as intended and prints the text in red, but the second println!
does not work as intended and prints the text in the default color.
It seems that the string literal loses the color information. I want to find a replace
equivalent that prints the text as I want.