zng::task::fs

Function read_to_string

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

Reads the entire contents of a file as a string.

This is a convenience function for reading entire files. It pre-allocates a string based on the file size when available, so it is typically faster than manually opening a file and reading from it.

If you want to read the contents as raw bytes, use read() instead.

§Errors

An error will be returned in the following situations:

  • path does not point to an existing file.
  • The current process lacks permissions to read the file.
  • The contents of the file cannot be read as a UTF-8 string.
  • Some other I/O error occurred.

§Examples

let contents = async_fs::read_to_string("a.txt").await?;