Function read
pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>>
fs only.Reads the entire contents of a file into a bytes vector.
This is an async version of std::fs::read.
This is a convenience function for using File::open and read_to_end with fewer imports and without an intermediate variable. It pre-allocates a buffer based on the file size when available, so it is generally faster than reading into a vector created with Vec::new().
This operation is implemented by running the equivalent blocking operation on a separate thread pool using spawn_blocking.
Errors
This function will return an error if path does not already exist. Other errors may also be returned according to OpenOptions::open.
It will also return an error if it encounters while reading an error of a kind other than ErrorKind::Interrupted.
io_uring support
On Linux, you can also use io_uring for executing system calls. To enable io_uring, you need to specify the --cfg tokio_unstable flag at compile time, enable the io-uring cargo feature, and set the Builder::enable_io_uring runtime option.
Support for io_uring is currently experimental, so its behavior may change or it may be removed in future versions.
Examples
use tokio::fs;
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
let contents = fs::read("address.txt").await?;
let foo: SocketAddr = String::from_utf8_lossy(&contents).parse()?;
Ok(())
}
MIT License
Copyright © Tokio Contributors
https://docs.rs/tokio/1.53.1/tokio/fs/fn.read.html