Function tracing::stdlib::fs::read_to_string

1.26.0 · source ·
pub fn read_to_string<P>(path: P) -> Result<String, Error>where
    P: AsRef<Path>,
Expand description

Read the entire contents of a file into a string.

This is a convenience function for using File::open and read_to_string 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.

It will also return an error if it encounters while reading an error of a kind other than io::ErrorKind::Interrupted, or if the contents of the file are not valid UTF-8.


use std::fs;
use std::net::SocketAddr;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;