Skip to content

Commit

Permalink
Merge pull request #1641 from FritteX14/fixed
Browse files Browse the repository at this point in the history
Simpler version of the read_lines script.
  • Loading branch information
marioidival authored Nov 12, 2022
2 parents 2afa5eb + 2475df9 commit 687a1c3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/std_misc/file/read_lines.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# `read_lines`

## Beginner friendly method
This method is NOT efficient. It's here for beginners
who can't understand the efficient method yet.

```rust,no_run
use std::fs::File;
use std::io::{ self, BufRead, BufReader };
fn read_lines(filename: String) -> io::Lines<BufReader<File>> {
// Open the file in read-only mode.
let file = File::open(filename).unwrap();
// Read the file line by line, and return an iterator of the lines of the file.
return io::BufReader::new(file).lines();
}
fn main() {
// Stores the iterator of lines of the file in lines variable.
let lines = read_lines("./hosts".to_string());
// Iterate over the lines of the file, and in this case print them.
for line in lines {
println!("{}", line.unwrap());
}
}
```

Running this program simply prints the lines individually.
```shell
$ 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
```

## Efficient method
The method `lines()` returns an iterator over the lines
of a file.

Expand Down

0 comments on commit 687a1c3

Please sign in to comment.