I'm trying to print a formatted string to stderr
in Rust (curious newbie here!), but it looks like an easy convenience macro (such as print!/println!
for stdout
) isn't provided in the standard library.
In C, one could just do it like this:
fprintf(stderr, "format_string", args ...);
I've successfully printed string literals to stderr
with
let stderr = std::io::stderr();
writeln!(&mut stderr, "literal");
The next thing that came into mind was to do a format!
first, and then use writeln!
, mut that fails to compile with error: expected a literal
. On the other hand, stderr.write()
expects &[u8]
, so using String::bytes().collect()
doesn't really work either..
What are the correct solutions to this?
See answer of the linked duplicate. Since Rust 1.19, you can use the eprintln!
macro.