Skip to content

Commit

Permalink
Added simpler version of the script
Browse files Browse the repository at this point in the history
  • Loading branch information
frittex14 authored Nov 10, 2022
1 parent 2b15c0a commit 2475df9
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

5 comments on commit 2475df9

@theredpea
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @frittex14 ,
I'm learning Rust. I like your new "beginner friendly" function -- I agree your function is simpler than the existing function :

  • your function does not wrap output in Result; your function unwraps the result, whereas the existing function propagates errors
  • your function does not use generics; your function expects a String, whereas the existing function expects anything that implements AsRef<Path>

But you say your "beginner friendly" function is not efficient , why not?
I think your function uses a BufReader and the BufReader.lines() method... I think both functions would be equally efficient?

@frittex14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey!
Actually, I've already covered this topic in issue #1677, but I understand your confusion. I've had a few people ask me about this before, so I'm definitely open to making changes. If you have any ideas on how to clarify this, feel free to open a PR. I've already proposed a change in the mentioned issue, so you can use that, or come up with something yourself. Either way, thank you for mentioning my slip.
Let me know if you have any other questions or concerns. Cheers!

@theredpea
Copy link

@theredpea theredpea commented on 2475df9 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @frittex14 ,

I like your suggested change in #1677 , mentioning "idiomatic" instead of "efficient".

Issue #1677 is helpful, as @woofyzhao says, this line is confusing.

This process is more efficient than creating a String in memory especially working with larger files.

"String in memory" suggests the "Beginner friendly" code creates a string in memory of the entire file contents.

EDIT Now I see how "This process is more efficient than creating a String in memory"... was there before your change!
It seems like it refers to your change; comparing "Beginner-friendly" to "Efficient". ... Instead, it meant to refer to an approach not shown at all!

It also looks like this pull request #1681 (from @arthurmilliken ) tries to address the issue
#1681 changes the "Beginner-friendly" method to make it less efficient and actually uses read_to_string() to "create a string in memory" But in my opinion, the code gets less beginner friendly!

So I wonder about removing mention of "efficiency" altogether, just focus on "beginner-friendly" / "simpler"

Anyway , thanks for acknowledging... If I can think of a better solution I'll try to make a PR (would be my first)!

@frittex14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually never noticed the #1681 PR. In my opinion, it tries to fix the issue from the wrong side, unnecessarily making it more difficult than it really is. But who am I to judge?
Also, don't hesitate or be afraid to make a pull request. We're a really friendly community, and no one will "laugh" at you for your proposals. Open as many PRs/issues as you can, and let's improve together! Thanks!

@theredpea
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, last comment, I learned there is pull request #1679 which I think does a good job of addressing my confusion.

Thanks again,

Please sign in to comment.