From bbf801d8e84ce7a4a8347ba450bf61b207a07077 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Fri, 26 Apr 2019 10:02:23 -0700 Subject: [PATCH 1/2] example to read a file line by line --- src/SUMMARY.md | 1 + src/std_misc/file/read_lines.md | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/std_misc/file/read_lines.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 6eca451bcc..2056c4381e 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -184,6 +184,7 @@ - [File I/O](std_misc/file.md) - [`open`](std_misc/file/open.md) - [`create`](std_misc/file/create.md) + - [`read lines`](std_misc/file/read_lines.md) - [Child processes](std_misc/process.md) - [Pipes](std_misc/process/pipe.md) - [Wait](std_misc/process/wait.md) diff --git a/src/std_misc/file/read_lines.md b/src/std_misc/file/read_lines.md new file mode 100644 index 0000000000..95b39f8f7c --- /dev/null +++ b/src/std_misc/file/read_lines.md @@ -0,0 +1,43 @@ +# Read Lines + +The method `lines()` returns an iterator over the lines +of a file. + +`File::open` expects a generic, `AsRef`. That's what +`read_lines()` expects as input. + +```rust,no_run +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +fn main() { + // File hosts must exist in current path before this produces output + if let Ok(lines) = read_lines("./hosts") { + // Consumes the iterator, returns an (Optional) String + for line in lines { + if let Ok(ip) = line { + println!("{}", ip); + } + } + } +} + +// The output is wrapped in a Result to allow matching on errors +// Returns an Iterator to the Reader of the lines of the file. +fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} +``` + +Running this program simply returns the lines individually. +```bash +$ echo -e "127.0.0.1\n192.168.0.1\n" > hosts +$ rustc read_lines.rs && ./read_lines +127.0.0.1 +192.168.0.1 +``` + +This process is more efficient that creating a `String` in memory for larger files. \ No newline at end of file From 6e44483a5772e1a9c51586d61d3b917afd52b1d7 Mon Sep 17 00:00:00 2001 From: Andrew Gauger Date: Fri, 26 Apr 2019 10:04:28 -0700 Subject: [PATCH 2/2] spelling --- src/std_misc/file/read_lines.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/std_misc/file/read_lines.md b/src/std_misc/file/read_lines.md index 95b39f8f7c..2a2f1855dc 100644 --- a/src/std_misc/file/read_lines.md +++ b/src/std_misc/file/read_lines.md @@ -32,7 +32,7 @@ where P: AsRef, { } ``` -Running this program simply returns the lines individually. +Running this program simply prints the lines individually. ```bash $ echo -e "127.0.0.1\n192.168.0.1\n" > hosts $ rustc read_lines.rs && ./read_lines @@ -40,4 +40,5 @@ $ rustc read_lines.rs && ./read_lines 192.168.0.1 ``` -This process is more efficient that creating a `String` in memory for larger files. \ No newline at end of file +This process is more efficient than creating a `String` in memory +especially working with larger files. \ No newline at end of file