Skip to content

Commit 9f8fa45

Browse files
committed
io docs
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent a7041be commit 9f8fa45

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

src/io/mod.rs

+57-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,74 @@
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.
27
//!
38
//! This module is an async version of [`std::io`].
49
//!
510
//! [`std::io`]: https://doc.rust-lang.org/std/io/index.html
611
//!
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
843
//!
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:
1048
//!
1149
//! ```no_run
50+
//! use async_std::prelude::*;
51+
//! use async_std::io::SeekFrom;
52+
//! use async_std::fs::File;
53+
//!
1254
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
1355
//! #
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?;
1561
//!
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]);
1966
//! #
2067
//! # Ok(()) }) }
2168
//! ```
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!
2272
2373
#[doc(inline)]
2474
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};

0 commit comments

Comments
 (0)