|
1 |
| -//! Basic input and output. |
| 1 | +//! Traits, helpers, and type definitions for core I/O functionality. |
| 2 | +//! |
| 3 | +//! The `async_std::io` module contains a number of common things you'll need |
| 4 | +//! when doing input and output. The most core part of this module is |
| 5 | +//! the [`Read`] and [`Write`] traits, which provide the |
| 6 | +//! most general interface for reading and writing input and output. |
2 | 7 | //!
|
3 | 8 | //! This module is an async version of [`std::io`].
|
4 | 9 | //!
|
5 | 10 | //! [`std::io`]: https://doc.rust-lang.org/std/io/index.html
|
6 | 11 | //!
|
7 |
| -//! # Examples |
| 12 | +//! # Read and Write |
| 13 | +//! |
| 14 | +//! Because they are traits, [`Read`] and [`Write`] are implemented by a number |
| 15 | +//! of other types, and you can implement them for your types too. As such, |
| 16 | +//! you'll see a few different types of I/O throughout the documentation in |
| 17 | +//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For |
| 18 | +//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on |
| 19 | +//! [`File`]s: |
| 20 | +//! |
| 21 | +//! ```no_run |
| 22 | +//! use async_std::prelude::*; |
| 23 | +//! use async_std::fs::File; |
| 24 | +//! |
| 25 | +//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 26 | +//! # |
| 27 | +//! let mut f = File::open("foo.txt").await?; |
| 28 | +//! let mut buffer = [0; 10]; |
| 29 | +//! |
| 30 | +//! // read up to 10 bytes |
| 31 | +//! let n = f.read(&mut buffer).await?; |
| 32 | +//! |
| 33 | +//! println!("The bytes: {:?}", &buffer[..n]); |
| 34 | +//! # |
| 35 | +//! # Ok(()) }) } |
| 36 | +//! ``` |
| 37 | +//! |
| 38 | +//! [`Read`] and [`Write`] are so important, implementors of the two traits have a |
| 39 | +//! nickname: readers and writers. So you'll sometimes see 'a reader' instead |
| 40 | +//! of 'a type that implements the [`Read`] trait'. Much easier! |
| 41 | +//! |
| 42 | +//! ## Seek and BufRead |
8 | 43 | //!
|
9 |
| -//! Read a line from the standard input: |
| 44 | +//! Beyond that, there are two important traits that are provided: [`Seek`] |
| 45 | +//! and [`BufRead`]. Both of these build on top of a reader to control |
| 46 | +//! how the reading happens. [`Seek`] lets you control where the next byte is |
| 47 | +//! coming from: |
10 | 48 | //!
|
11 | 49 | //! ```no_run
|
| 50 | +//! use async_std::prelude::*; |
| 51 | +//! use async_std::io::SeekFrom; |
| 52 | +//! use async_std::fs::File; |
| 53 | +//! |
12 | 54 | //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
13 | 55 | //! #
|
14 |
| -//! use async_std::io; |
| 56 | +//! let mut f = File::open("foo.txt").await?; |
| 57 | +//! let mut buffer = [0; 10]; |
| 58 | +//! |
| 59 | +//! // skip to the last 10 bytes of the file |
| 60 | +//! f.seek(SeekFrom::End(-10)).await?; |
15 | 61 | //!
|
16 |
| -//! let stdin = io::stdin(); |
17 |
| -//! let mut line = String::new(); |
18 |
| -//! stdin.read_line(&mut line).await?; |
| 62 | +//! // read up to 10 bytes |
| 63 | +//! let n = f.read(&mut buffer).await?; |
| 64 | +//! |
| 65 | +//! println!("The bytes: {:?}", &buffer[..n]); |
19 | 66 | //! #
|
20 | 67 | //! # Ok(()) }) }
|
21 | 68 | //! ```
|
| 69 | +//! |
| 70 | +//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but |
| 71 | +//! to show it off, we'll need to talk about buffers in general. Keep reading! |
22 | 72 |
|
23 | 73 | #[doc(inline)]
|
24 | 74 | pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};
|
|
0 commit comments