file_into_string

Crates.iofile_into_string
lib.rsfile_into_string
version1.1.1
sourcesrc
created_at2022-10-12 21:58:33.018295
updated_at2022-10-12 22:01:00.647581
descriptionfile_into_string: Rust crate of utility functions to read a file into a string, or a vector of strings, and preserving line endings.
homepage
repositoryhttps://github.com/sixarm/file-into-string-rust-crate/
max_upload_size
id686727
size7,400
Joel Parker Henderson (joelparkerhenderson)

documentation

README

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

Commit count: 16

cargo fmt