Crates.io | read_lines_into |
lib.rs | read_lines_into |
version | 2.0.0 |
source | src |
created_at | 2022-10-14 01:22:48.474633 |
updated_at | 2022-10-14 01:22:48.474633 |
description | Read lines (from a Path, File, etc.) into a struct (a String, a Vec |
homepage | |
repository | https://github.com/sixarm/read-lines-into-rust-crate/ |
max_upload_size | |
id | 687857 |
size | 36,376 |
Read lines (from Path, File, BufRead) into a struct (String, Vec
Example:
// Choose any existing text file
let path = Path::new("example.txt");
// Read lines from the path's file into a string
let string = path.read_lines_into_string().unwrap();
// Read lines from the path's file into a vector of strings
let strings = path.read_lines_into_vec_string().unwrap();
Add dependency:
[dependencies]
read_lines_into = "*"
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.
Unix systems typically end text lines with \n
LINE FEED (LF).
Windows systems typically end text lines with \r
CARRIAGE RETURN (CR)
then and \n
LINE FEED (LF).
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.
Rust std::io::BufRead
and its function lines()
.
Rust std::include_str
and its macro include_string!
.
Rust crate load_file
and its macro load_str!
.
Rust std::fs::read_to_string(file_name).unwrap().lines()
.