#read-file #read-line #file #line-ending #read #line #text-file

file_into_string

file_into_string: Rust crate of utility functions to read a file into a string, or a vector of strings, and preserving line endings

2 stable releases

1.1.1 Oct 12, 2022

#475 in Testing

MIT OR Apache-2.0 OR GPL-2.0 OR GPL-3.0

8KB

file_into_string Rust crate

Read a typical text file into a string or vector of strings.

  • file_into_string(file: File) -> std::io::Result<String>

  • file_into_strings(file: File) -> std::io::Result<Vec<String>>

Examples:

use std::fs::File;
use file_into_string::*;

// Open an existing text file; read the File into a String.
let file = File::open("example.txt").unwrap();
let string = file_into_string(file).unwrap();
 
// Open an existing text file; read the File into a Vec<String>.
let file = File::open("example.txt").unwrap();
let strings = file_into_strings(file).unwrap();

Install

You can use this Rust crate:

[dependencies]
file_into_string = "*"

Or if you prefer, you can copy the source code into your own program.

Notes

These functions deliberately preserve line endings, which are \n newlines and \r carriage returns.

These functions use buffered readers for efficiency.

These functions are written to be easy to understand, so you can copy them into your own code if you wish.

If you're reading very large files, then you may prefer to write your own code to process each line as it's read.

FAQ

Why use this instead of the Rust BufRead lines() function?

Because we have use cases where we must preserve line endings.

Why publish this as a crate?

Because we want to make it easy to use, and easy to show as examples for developers who are learning how to program using Rust.

Tracking

No runtime deps