-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a simpler version of read_lines #1480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
While a good step in the direction of a simpler version FritteX14's solution doesn't solve all the concerns in this issue, it is still using |
The current examples are still confusing for newcomers, especially because both version use
|
I think you are basing you feedback on the rust-by-example book, if you look at the source you see that comment still applies.
Good point, a look around this repo show most file examples use match to handle errors for example: let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
}; It might be better to match that style. However rust by example is not only for beginners replacing unwrap with match makes it harder to copy the code and build on it. Using
Most of my non library code does not use
IMO if we add a third example centered around performance it should clearly note that this degrades readability and is usually the wrong approach. We might also want to proof it actually improves performance using a benchmark. At which point it get to complicated for It would fit the rust performance book. A link to that might be nice here. |
Ah indeed I was looking at doc.rust-lang.org, the git version makes a lot more sense. Is it a matter of waiting for the next deployment on d.r-l.o ?
Not sure what is the best style to adopt, there are good arguments for
I think both AsRef and read_until make sense in a third example with a foreword that they might be overkill and that the second example is often good enough. But they're both pretty classic tweaks for flexible API (AsRef) and speedier parsing (read_until). Delaying the |
Hi, I'm closing this issue just to clean up the items that already exist. If you think this issue makes sense to stay open, please create a new issue with updated information and mention this one. thanks! |
Hello! First of all thanks for the amazing resource!
The read_lines example features an optimized way of reading lines from a file with a
BufReader
.The code is efficient but is pretty involved and makes a newcomer stumble, forcing them to read about
Result
s,BufReader
s,AsRef
and generics with awhere
clause.My new-to-rust friend ran into this when trying to solve challenges from Advent of Code.
Most challenges from AoC require reading a sequence of lines from an input file and I feel it would be good to also have a less efficient example that exposes less new concepts to a newcomer, because this is the first thing you run into when trying to solve an AoC challenge.
Example
or
I am still not happy about having to mention
&str
toString
conversion but I feel like even though these examples are longer they apply less cognitive pressure to someone not familiar with rustThe text was updated successfully, but these errors were encountered: