Crates.io | file_into_string |
lib.rs | file_into_string |
version | 1.1.1 |
source | src |
created_at | 2022-10-12 21:58:33.018295 |
updated_at | 2022-10-12 22:01:00.647581 |
description | file_into_string: Rust crate of utility functions to read a file into a string, or a vector of strings, and preserving line endings. |
homepage | |
repository | https://github.com/sixarm/file-into-string-rust-crate/ |
max_upload_size | |
id | 686727 |
size | 7,400 |
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();
You can use this Rust crate:
[dependencies]
file_into_string = "*"
Or if you prefer, you can copy the source code into your own program.
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.
BufRead lines()
function?Because we have use cases where we must preserve line endings.
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.