I'm Rust newbie and am trying to understand how to use ansi_term to create a string that contains a few different escape sequences.
I have the following code:
use ansi_term::ANSIString;
use ansi_term::Colour::{Blue, Red};
fn main() {
let some_value = format!("{:b}", 42);
let strings: &[ANSIString<'static>] = &[
Red.paint("["),
Blue.bold().paint(some_value),
Red.paint("]"),
];
println!("{}", get_display(strings));
}
fn get_display(strings: &[ANSIString<'static>]) -> String {
let mut display = String::new();
for s in strings {
// Double to_string() isn't strictly necessary, but otherwise clippy complains
display.push_str(&s.to_string());
display.push('\n');
}
display
}
and, while it works, when I run cargo clippy
on it, I get the following:
warning: unnecessary use of `to_string`
--> src/main.rs:19:26
|
19 | display.push_str(&s.to_string());
| ^^^^^^^^^^^^^^ help: use: `s`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
= note: `#[warn(clippy::unnecessary_to_owned)]` on by default
warning: `ansi-color-test` (bin "ansi-color-test") generated 1 warning
Of course, if I remove the to_string()
the code works, but I don't get ansi escape sequences, so no color.
If, on the other hand, I add another to_string()
, everything works and clippy doesn't complain.
What is going on? From Type Definition ansi_term::ANSIString, I found "Although not technically a string itself, it can be turned into one with the to_string
method."
(Since I'm a newbie, feel free to point me to a better place to ask this question.)