|
21 | 21 | /// # Examples
|
22 | 22 | ///
|
23 | 23 | /// ```
|
24 |
| -/// use std::io::{self, Write}; |
| 24 | +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 25 | +/// # |
| 26 | +/// use async_std::prelude::*; |
| 27 | +/// use async_std::io; |
| 28 | +/// use async_std::print; |
25 | 29 | ///
|
26 |
| -/// print!("this "); |
27 |
| -/// print!("will "); |
28 |
| -/// print!("be "); |
29 |
| -/// print!("on "); |
30 |
| -/// print!("the "); |
31 |
| -/// print!("same "); |
32 |
| -/// print!("line "); |
| 30 | +/// print!("this ").await; |
| 31 | +/// print!("will ").await; |
| 32 | +/// print!("be ").await; |
| 33 | +/// print!("on ").await; |
| 34 | +/// print!("the ").await; |
| 35 | +/// print!("same ").await; |
| 36 | +/// print!("line ").await; |
33 | 37 | ///
|
34 |
| -/// io::stdout().flush().unwrap(); |
| 38 | +/// io::stdout().flush().await.unwrap(); |
35 | 39 | ///
|
36 |
| -/// print!("this string has a newline, why not choose println! instead?\n"); |
| 40 | +/// print!("this string has a newline, why not choose println! instead?\n").await; |
37 | 41 | ///
|
38 |
| -/// io::stdout().flush().unwrap(); |
| 42 | +/// io::stdout().flush().await.unwrap(); |
| 43 | +/// # |
| 44 | +/// # Ok(()) }) } |
39 | 45 | /// ```
|
40 | 46 | #[macro_export]
|
41 | 47 | macro_rules! print {
|
42 |
| - ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); |
| 48 | + ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))) |
| 49 | +} |
| 50 | + |
| 51 | +/// Prints to the standard output, with a newline. |
| 52 | +/// |
| 53 | +/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone |
| 54 | +/// (no additional CARRIAGE RETURN (`\r`/`U+000D`)). |
| 55 | +/// |
| 56 | +/// Use the [`format!`] syntax to write data to the standard output. |
| 57 | +/// See [`std::fmt`] for more information. |
| 58 | +/// |
| 59 | +/// Use `println!` only for the primary output of your program. Use |
| 60 | +/// [`eprintln!`] instead to print error and progress messages. |
| 61 | +/// |
| 62 | +/// [`format!`]: macro.format.html |
| 63 | +/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html |
| 64 | +/// [`eprintln!`]: macro.eprintln.html |
| 65 | +/// # Panics |
| 66 | +/// |
| 67 | +/// Panics if writing to `io::stdout` fails. |
| 68 | +/// |
| 69 | +/// # Examples |
| 70 | +/// |
| 71 | +/// ``` |
| 72 | +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 73 | +/// # |
| 74 | +/// use async_std::println; |
| 75 | +/// |
| 76 | +/// println!().await; // prints just a newline |
| 77 | +/// println!("hello there!").await; |
| 78 | +/// println!("format {} arguments", "some").await; |
| 79 | +/// # |
| 80 | +/// # Ok(()) }) } |
| 81 | +/// ``` |
| 82 | +#[macro_export] |
| 83 | +macro_rules! println { |
| 84 | + () => ($crate::print!("\n")); |
| 85 | + ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))) |
| 86 | +} |
| 87 | + |
| 88 | +/// Prints to the standard error. |
| 89 | +/// |
| 90 | +/// Equivalent to the [`print!`] macro, except that output goes to |
| 91 | +/// [`io::stderr`] instead of `io::stdout`. See [`print!`] for |
| 92 | +/// example usage. |
| 93 | +/// |
| 94 | +/// Use `eprint!` only for error and progress messages. Use `print!` |
| 95 | +/// instead for the primary output of your program. |
| 96 | +/// |
| 97 | +/// [`io::stderr`]: io/struct.Stderr.html |
| 98 | +/// [`print!`]: macro.print.html |
| 99 | +/// |
| 100 | +/// # Panics |
| 101 | +/// |
| 102 | +/// Panics if writing to `io::stderr` fails. |
| 103 | +/// |
| 104 | +/// # Examples |
| 105 | +/// |
| 106 | +/// ``` |
| 107 | +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 108 | +/// # |
| 109 | +/// use async_std::eprint; |
| 110 | +/// |
| 111 | +/// eprint!("Error: Could not complete task").await; |
| 112 | +/// # |
| 113 | +/// # Ok(()) }) } |
| 114 | +/// ``` |
| 115 | +#[macro_export] |
| 116 | +macro_rules! eprint { |
| 117 | + ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*))) |
| 118 | +} |
| 119 | + |
| 120 | +/// Prints to the standard error, with a newline. |
| 121 | +/// |
| 122 | +/// Equivalent to the [`println!`] macro, except that output goes to |
| 123 | +/// [`io::stderr`] instead of `io::stdout`. See [`println!`] for |
| 124 | +/// example usage. |
| 125 | +/// |
| 126 | +/// Use `eprintln!` only for error and progress messages. Use `println!` |
| 127 | +/// instead for the primary output of your program. |
| 128 | +/// |
| 129 | +/// [`io::stderr`]: io/struct.Stderr.html |
| 130 | +/// [`println!`]: macro.println.html |
| 131 | +/// |
| 132 | +/// # Panics |
| 133 | +/// |
| 134 | +/// Panics if writing to `io::stderr` fails. |
| 135 | +/// |
| 136 | +/// # Examples |
| 137 | +/// |
| 138 | +/// ``` |
| 139 | +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 140 | +/// # |
| 141 | +/// use async_std::eprintln; |
| 142 | +/// |
| 143 | +/// eprintln!("Error: Could not complete task").await; |
| 144 | +/// # |
| 145 | +/// # Ok(()) }) } |
| 146 | +/// ``` |
| 147 | +#[macro_export] |
| 148 | +macro_rules! eprintln { |
| 149 | + () => (async { $crate::eprint!("\n").await; }); |
| 150 | + ($($arg:tt)*) => ( |
| 151 | + async { |
| 152 | + $crate::io::_eprint(format_args!($($arg)*)).await; |
| 153 | + } |
| 154 | + ); |
43 | 155 | }
|
0 commit comments