Function tracing::stdlib::fs::read

1.26.0 · source ·
pub fn read<P>(path: P) -> Result<Vec<u8, Global>, Error>where
    P: AsRef<Path>,
Expand description

Read the entire contents of a file into a bytes vector.

This is a convenience function for using File::open and read_to_end with fewer imports and without an intermediate variable.


This function will return an error if path does not already exist. Other errors may also be returned according to OpenOptions::open.

While reading from the file, this function handles io::ErrorKind::Interrupted with automatic retries. See io::Read documentation for details.


use std::fs;
use std::net::SocketAddr;

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
    let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;