Helper functions
Additionally, just like std
, the tokio::io
module contains a number of helpful utility functions as well as APIs for working with standard input, standard output and standard error. For example, tokio::io::copy
asynchronously copies the entire contents of a reader into a writer.
use tokio::fs::File;
use tokio::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut reader: &[u8] = b"hello";
let mut file = File::create("foo.txt").await?;
io::copy(&mut reader, &mut file).await?;
Ok(())
}
Note that this uses the fact that byte arrays also implement AsyncRead
.