Last active
June 29, 2016 20:05
-
-
Save jwilm/16ce667076b854fbae55568f340c6a84 to your computer and use it in GitHub Desktop.
Improved println!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Improved println! macro that allows specifying stdout or stderr | |
/// | |
/// # Examples | |
/// | |
/// Printing to stdout works like normal | |
/// ```rust | |
/// zprintln!("Hello, zprintln!"); | |
/// ``` | |
/// | |
/// Printing to stderr works by passing a special argument | |
/// ```rust | |
/// zprintln!(stderr, "Hello, stderr!"); | |
/// ``` | |
macro_rules! zprintln { | |
(stdout, $($arg:tt)*) => { | |
println!($($arg)*); | |
}; | |
(stderr, $($arg:tt)*) => {{ | |
use std::io::Write; | |
writeln!(::std::io::stderr(), $($arg)*).unwrap(); | |
}}; | |
($($arg:tt)*) => { | |
println!($($arg)*); | |
}; | |
} | |
fn main() { | |
zprintln!("normal shows up on stdout"); | |
zprintln!("normal shows up on stdout with arg: {}", 1); | |
zprintln!(stdout, "with stdout goes to stdout: {}", 1); | |
zprintln!(stderr, "with stderr goes to stderr: {}", 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: