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.

  1. use tokio::fs::File;
  2. use tokio::io;
  3. #[tokio::main]
  4. async fn main() -> io::Result<()> {
  5. let mut reader: &[u8] = b"hello";
  6. let mut file = File::create("foo.txt").await?;
  7. io::copy(&mut reader, &mut file).await?;
  8. Ok(())
  9. }

Note that this uses the fact that byte arrays also implement AsyncRead.