|
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 | | -//! |
175 | 125 | //! ## Iterator types |
176 | 126 | //! |
177 | 127 | //! A large number of the structures provided by `std::io` are for various |
|
204 | 154 | //! |
205 | 155 | //! ```no_run |
206 | 156 | //! use async_std::io; |
| 157 | +//! use async_std::fs::File; |
207 | 158 | //! |
208 | 159 | //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
209 | 160 | //! # |
210 | | -//! io::copy(&mut io::stdin(), &mut io::stdout()).await?; |
| 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?; |
211 | 165 | //! # |
212 | 166 | //! # Ok(()) }) } |
213 | 167 | //! ``` |
|
224 | 178 | //! ``` |
225 | 179 | //! #![allow(dead_code)] |
226 | 180 | //! use async_std::io; |
| 181 | +//! use std::time::Duration; |
227 | 182 | //! |
228 | 183 | //! async fn read_input() -> io::Result<()> { |
229 | | -//! let mut input = String::new(); |
230 | | -//! |
231 | | -//! io::stdin().read_line(&mut input).await?; |
| 184 | +//! let f = io::timeout(Duration::from_secs(5), async { |
| 185 | +//! Ok(()) |
| 186 | +//! }); |
232 | 187 | //! |
233 | | -//! println!("You typed: {}", input.trim()); |
| 188 | +//! assert_eq!(f.await?, ()); |
234 | 189 | //! |
235 | 190 | //! Ok(()) |
236 | 191 | //! } |
|
260 | 215 | //! [`BufReader`]: struct.BufReader.html |
261 | 216 | //! [`BufWriter`]: struct.BufWriter.html |
262 | 217 | //! [`Write::write`]: trait.Write.html#tymethod.write |
263 | | -//! [`io::stdout`]: fn.stdout.html |
264 | | -//! [`println!`]: ../macro.println.html |
265 | 218 | //! [`Lines`]: struct.Lines.html |
266 | 219 | //! [`io::Result`]: type.Result.html |
267 | 220 | //! [`?` operator]: https://doc.rust-lang.org/stable/book/appendix-02-operators.html |
@@ -305,24 +258,7 @@ cfg_std! { |
305 | 258 | } |
306 | 259 |
|
307 | 260 | 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}; |
315 | 261 | pub use timeout::timeout; |
316 | 262 |
|
317 | 263 | 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; |
328 | 264 | } |
0 commit comments