|
122 | 122 | //! # Ok(()) }) } |
123 | 123 | //! ``` |
124 | 124 | //! |
| 125 | +//! ## Standard input and output |
| 126 | +//! |
| 127 | +//! A very common source of input is standard input: |
| 128 | +//! |
| 129 | +//! ```no_run |
| 130 | +//! use async_std::io; |
| 131 | +//! |
| 132 | +//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 133 | +//! # |
| 134 | +//! let mut input = String::new(); |
| 135 | +//! |
| 136 | +//! io::stdin().read_line(&mut input).await?; |
| 137 | +//! |
| 138 | +//! println!("You typed: {}", input.trim()); |
| 139 | +//! # |
| 140 | +//! # Ok(()) }) } |
| 141 | +//! ``` |
| 142 | +//! |
| 143 | +//! Note that you cannot use the [`?` operator] in functions that do not return |
| 144 | +//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`] |
| 145 | +//! or `match` on the return value to catch any possible errors: |
| 146 | +//! |
| 147 | +//! ```no_run |
| 148 | +//! use async_std::io; |
| 149 | +//! |
| 150 | +//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 151 | +//! # |
| 152 | +//! let mut input = String::new(); |
| 153 | +//! |
| 154 | +//! io::stdin().read_line(&mut input).await.unwrap(); |
| 155 | +//! # |
| 156 | +//! # Ok(()) }) } |
| 157 | +//! ``` |
| 158 | +//! |
| 159 | +//! And a very common source of output is standard output: |
| 160 | +//! |
| 161 | +//! ```no_run |
| 162 | +//! use async_std::io; |
| 163 | +//! use async_std::io::prelude::*; |
| 164 | +//! |
| 165 | +//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 166 | +//! # |
| 167 | +//! io::stdout().write(&[42]).await?; |
| 168 | +//! # |
| 169 | +//! # Ok(()) }) } |
| 170 | +//! ``` |
| 171 | +//! |
| 172 | +//! Of course, using [`io::stdout`] directly is less common than something like |
| 173 | +//! [`println!`]. |
| 174 | +//! |
125 | 175 | //! ## Iterator types |
126 | 176 | //! |
127 | 177 | //! A large number of the structures provided by `std::io` are for various |
|
154 | 204 | //! |
155 | 205 | //! ```no_run |
156 | 206 | //! use async_std::io; |
157 | | -//! use async_std::fs::File; |
158 | 207 | //! |
159 | 208 | //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
160 | 209 | //! # |
161 | | -//! let mut reader: &[u8] = b"hello"; |
162 | | -//! let mut writer = File::open("foo.txt").await?; |
163 | | -//! |
164 | | -//! io::copy(&mut reader, &mut writer).await?; |
| 210 | +//! io::copy(&mut io::stdin(), &mut io::stdout()).await?; |
165 | 211 | //! # |
166 | 212 | //! # Ok(()) }) } |
167 | 213 | //! ``` |
|
178 | 224 | //! ``` |
179 | 225 | //! #![allow(dead_code)] |
180 | 226 | //! use async_std::io; |
181 | | -//! use std::time::Duration; |
182 | 227 | //! |
183 | 228 | //! async fn read_input() -> io::Result<()> { |
184 | | -//! let f = io::timeout(Duration::from_secs(5), async { |
185 | | -//! Ok(()) |
186 | | -//! }); |
| 229 | +//! let mut input = String::new(); |
| 230 | +//! |
| 231 | +//! io::stdin().read_line(&mut input).await?; |
187 | 232 | //! |
188 | | -//! assert_eq!(f.await?, ()); |
| 233 | +//! println!("You typed: {}", input.trim()); |
189 | 234 | //! |
190 | 235 | //! Ok(()) |
191 | 236 | //! } |
|
215 | 260 | //! [`BufReader`]: struct.BufReader.html |
216 | 261 | //! [`BufWriter`]: struct.BufWriter.html |
217 | 262 | //! [`Write::write`]: trait.Write.html#tymethod.write |
| 263 | +//! [`io::stdout`]: fn.stdout.html |
| 264 | +//! [`println!`]: ../macro.println.html |
218 | 265 | //! [`Lines`]: struct.Lines.html |
219 | 266 | //! [`io::Result`]: type.Result.html |
220 | 267 | //! [`?` operator]: https://doc.rust-lang.org/stable/book/appendix-02-operators.html |
@@ -258,7 +305,24 @@ cfg_std! { |
258 | 305 | } |
259 | 306 |
|
260 | 307 | cfg_default! { |
| 308 | + // For use in the print macros. |
| 309 | + #[doc(hidden)] |
| 310 | + pub use stdio::{_eprint, _print}; |
| 311 | + |
| 312 | + pub use stderr::{stderr, Stderr}; |
| 313 | + pub use stdin::{stdin, Stdin}; |
| 314 | + pub use stdout::{stdout, Stdout}; |
261 | 315 | pub use timeout::timeout; |
262 | 316 |
|
263 | 317 | mod timeout; |
| 318 | + mod stderr; |
| 319 | + mod stdin; |
| 320 | + mod stdio; |
| 321 | + mod stdout; |
| 322 | +} |
| 323 | + |
| 324 | +cfg_unstable_default! { |
| 325 | + pub use stderr::StderrLock; |
| 326 | + pub use stdin::StdinLock; |
| 327 | + pub use stdout::StdoutLock; |
264 | 328 | } |
0 commit comments