How to send output to stderr?

后端 未结 5 1940
感动是毒
感动是毒 2020-12-15 02:55

One uses this to send output to stdout:

println!(\"some output\")

I think there is no corresponding macro to do the same for stderr.

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 03:57

    print! and println! are convenience methods for writing to standard output. There are other macros with the same formatting features available for different tasks:

    • write! and writeln! to write a formatted string to a &mut Writer
    • format! to just generate a formatted String

    To write to the standard error stream, you can use e.g. writeln! like this:

    use std::io::Write;
    
    fn main() {
        let mut stderr = std::io::stderr();
        writeln!(&mut stderr, "Error!").unwrap();
    }
    

提交回复
热议问题