First Rust Macro

by  Tim Henderson

I started learning Rust yesterday. Today as I was writing input handling code I wanted to print to the standard err. The way to do that seems to be:

(writeln![io::stderr(), "{}", err]).ok().expect("write to stderr failed");

That seems a bit verbose to me versus printing to the standard out, which is:

println!("{}", err)

So I decided to fix that with the following macro:

macro_rules! log(($fmt:expr$(, $msg:expr)*) => {
    (writeln![io::stderr(), $fmt $(, $msg)*]).ok().expect("log failed")
})

Which you can use like so:

log!("{}", err)

Or

log!("expected {} got {}", expected, actual)