Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. /// Improved println! macro that allows specifying stdout or stderr
  2. ///
  3. /// # Examples
  4. ///
  5. /// Printing to stdout works like normal
  6. /// ```rust
  7. /// zprintln!("Hello, zprintln!");
  8. /// ```
  9. ///
  10. /// Printing to stderr works by passing a special argument
  11. /// ```rust
  12. /// zprintln!(stderr, "Hello, stderr!");
  13. /// ```
  14. macro_rules! zprintln {
  15. (stdout, $($arg:tt)*) => {
  16. println!($($arg)*);
  17. };
  18. (stderr, $($arg:tt)*) => {{
  19. use std::io::Write;
  20. writeln!(::std::io::stderr(), $($arg)*).unwrap();
  21. }};
  22. ($($arg:tt)*) => {
  23. println!($($arg)*);
  24. };
  25. }
  26.  
  27. fn main() {
  28. zprintln!("normal shows up on stdout");
  29. zprintln!("normal shows up on stdout with arg: {}", 1);
  30. zprintln!(stdout, "with stdout goes to stdout: {}", 1);
  31. zprintln!(stderr, "with stderr goes to stderr: {}", 2);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement