News for hackers. Stuff that matters.
Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @07:31PM (#13585810)
Here's a wart. Why is reading lines from a file so hard? It's one of the first things people are going to need to do in a programming language.

On top of handling errors (which I understand is necessary, and which the ? operator makes easier), it requires importing BufReader and BufRead, and wrapping a reference to a file handle in BufReader.

Nobody is going to know how to do this unless they come across it in Rust By Example or on Stack Overflow. Shouldn't a standard library be able to abstract over fiddly details like this? Why is it my job to tell it how to buffer?

I say this as someone who is enthusiastic about Rust, but still learning.

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @07:46PM (#13585902)
> Why is reading lines from a file so hard?

It's not so much that it's hard, as is there's lots of options. Can you afford to read it all into memory as a String? Do you need to only read it bit by bit?

> Why is it my job to tell it how to buffer?

Systems langauges need to expose these kinds of details and levels of control.

> Nobody is going to know how to do this

I googled "rust open a file and read by lines" and got these results:

https://users.rust-lang.org/t/read-a-file-line-by-line/1585

https://doc.rust-lang.org/std/io/

https://doc.rust-lang.org/std/fs/struct.File.html

The first two show you directly how, though the first one is dealing with the error message. The last one shows reading it all in. I have some more work to do :)

Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @07:53PM (#13585941)
It would be silly and wasteful to read the whole file into memory just so I can iterate its lines. I'm not sure why that's an option you'd need to accomodate.

However, many languages make a reasonable assumption that you can afford to fit each line in memory, and provide an obvious way to do this.

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @07:57PM (#13585976)
> I'm not sure why that's an option you'd need to accomodate.

Again, it's about control. Maybe you're only loading a small configuration file, and so fetching it all in one go is better than dealing with a buffer.

> an obvious way to do this.

If you search for 'line' or 'lines' in rustdoc, the correct thing is right near the top, which will show you how to use it with BufRead.

Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @08:09PM (#13586031)
> Maybe you're only loading a small configuration file, and so fetching it all in one go is better than dealing with a buffer.

If it's small, it can't be that much worse to buffer. What's wrong with reasonable defaults?

Can you tell me what it means to search for 'line' or 'lines' in rustdoc? Is this a command-line tool? I'm aware of the 'rustdoc' that generates documentation, but not of anything by that name that searches documentation, and Googling isn't turning up much of anything except the Rust book here.

It would be great to have an offline way to search Rust documentation and examples, as things like this make it very hard to write Rust on a plane, for example.

Googling often gives answers that are wrong or pre-1.0. You seem to have better luck at searching for the right things, perhaps because you are an expert in the language and know exactly what to search for.

(Amusing related anecdote: I googled for "how to return self in rust" and got a suicide prevention website. Jeez, Google, it's not that bad.)

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @08:14PM (#13586066)
> Can you tell me what it means to search for 'line' or 'lines' in rustdoc?

Rustdoc has a search bar at the top. with 'line' it's the third result, 'lines' is the first https://doc.rust-lang.org/stable/std/?search=lines

and the short description makes it clear that the first two results are irrelevant in this case.

> It would be great to have an offline way to search Rust documentation and examples,

It all works offline. These docs are pre-installed for you when you install Rust, and 'cargo doc' will generate them for your whole project.

> perhaps because you are an expert in the language and know exactly what to search for.

I copy-pasted my search term exactly in the previous post.

Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @09:10PM (#13586363)
> Rustdoc has a search bar at the top.

Let me assure you that I mean only the best for this language and I appreciate your persistent willingness to help, but I want to help debug your use of internal terminology that doesn't help beginners.

"rustdoc" is not how you should be telling beginners to look for documentation. As far as I can tell, "rustdoc" is the tool for generating documentation, not searching it. I assume you use rustdoc a lot and that's why the name comes to mind.

Maybe the term is overloaded, but I cannot find anything called "rustdoc" that involves a search bar. Googling for "rustdoc" gives many forms of Rust documentation, none of which have a search bar. DuckDuckGo-ing for "rustdoc" (as some documentation suggests instead of Googling) gets me some of the same things, plus rustdoc.com, which is a big honking security warning on top of someone's broken personal blog.

I know that exact link you gave me includes a search bar, but that's an example of finding the answer because you already know it.

And if you know to go to doc.rust-lang.org and go to the Standard Library API Reference, you get a search bar, but that's not where I would think of going to answer the question "how do I iterate lines".

That's why I end up Googling and getting to Stack Overflow, full of wrong answers and someone named Shepmaster yelling at newbies.

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @09:24PM (#13586431)
As I said in the other thread, I truly appreciate it. :)

Sorry, as I mentioned elsewhere I was in a meeting; I should have just waited to apply to you. It's true that "rustdoc" is overloaded, people use it to mean "rustdoc's output" which would mean the standard library docs in this case. My bad!

Re:Rust's 2017 Roadmap by shepmaster on Thursday February 09, 2017 @08:05PM (#13612504)
o/ waves

Please make sure to leave a comment and/or downvote any answers that are wrong to help people coming after you. I also apologize for whichever specific way I harmed you.

Re:Rust's 2017 Roadmap by gphilip on Monday February 06, 2017 @08:41PM (#13586201)
> It would be great to have an offline way to search Rust documentation and examples,

>> It all works offline. These docs are pre-installed for you when you install Rust, and 'cargo doc' will generate them for your whole project.

Not by default, anymore[0], as I found out when I wanted to read the book offline.

To set up offline documentation, do:

  rustup component add rust-docs # One-time thing
  
Now to open the docs in a browser, do one of:

  rustup doc
  rustup doc --api
  rustup doc --book
The second and third forms open up the API documentation and the book, respectively. Bookmark for one-click access.

[0] https://users.rust-lang.org/t/psa-rust-documentation-is-now-...

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @09:20PM (#13586411)
Gah, I always forget. I want that switched back.
Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @08:20PM (#13586095)
> I copy-pasted my search term exactly in the previous post.

I'm bringing up a problem with how the language presents itself to beginners, not asking you to solve a specific problem for me. If searching is the answer to everything, then there needs to be a concerted effort to take Google-juice away from bad or deprecated answers.

Incidentally, the search result you're describing for "lines" says "An iterator over the lines of an instance of BufRead.", which only appears to be the answer to "how do I iterate lines of a file?" if you already know what the answer is.

Re:Rust's 2017 Roadmap by steveklabnik on Monday February 06, 2017 @09:22PM (#13586420)
No no, I very much appreciate it!

I think we have two searches confused. When I meant "my exact query, when we were taking about Google, here: https://news.ycombinator.com/item?id=13585902

I find it interesting that we got different results.

I thought "an iterator over lines" would be enough; maybe not.

Re:Rust's 2017 Roadmap by douche on Monday February 06, 2017 @07:36PM (#13585842)
That's a good point. It's slightly reminiscent of Haskell, where you have to start trying to understand the IO monad and do notation to do the most trivial real-world examples.

In other languages, you might do File.ReadAllLines("foo.txt"), and bam, you're done.

Re:Rust's 2017 Roadmap by dripton on Monday February 06, 2017 @07:58PM (#13585986)
Unless foo.txt is too big, in which case, bam, out of memory.
Re:Rust's 2017 Roadmap by sametmax on Tuesday February 07, 2017 @08:33AM (#13589189)
No, Python, ruby and such have a common interface for all lazy iterations.

In Python you can just loop on anything declaring the __iter__ method, and it will lazily yield results little by little.

IO related objects implement it plus an additional layer of interface so that you can do:

    with open('file', [mode, encoding]) as f:
To get an auto closing file handle and then choose:

- for `line in f` to lazy read it line by line. This calls __iter__.

- `f.read([byte_count])` to read it all or some bunch of bytes. `f.seek(index)` to move around, etc.

- `f.readlines()` to get a list of all lines in memory.

- `f.close()` if you wish to close the file manually instead of letting the `with` keyword doing it work you.

This interface works for most IO, including files, sockets, in memory buffers, etc.

So you can choose an automatic lazy loading, a manual loading, load everything in memory, etc. And still have a lot of control.

I think rust should get some traits to expose such a common high level interface on top of the current way it deals with files, to ease simple operations. Just make sure the documentation states what you can do to go lower level.

There is a similar trend with many basic topics trying to do that as 3rd party libs too. click makes creating cmd UI very easy on top of argparse which is lower level. pendulum is higher level than datetime. requests higher than urllib. etc. Those all make doing those very common operation super easy.

Now rust is not meant to be Python/Ruby/whatever, being very low level, and checking safety at compile time implies very different requirements.

But those communities have some good concepts on API ergonomics and it would be a shame to not steal ideas from them.

Re:Rust's 2017 Roadmap by kazagistar on Tuesday February 07, 2017 @10:56AM (#13590152)
The Rust team seems to not want to write trait impls where it would be inefficient to use those traits. If a method doesn't exist, its often because there is a slightly better way. They also insist that memory usage, especially any heap allocations, be very clear and explicit: allocating and attaching a buffer is probably not OK within the internal logic of the library designs.

The interface for reading is split into multiple parts. Read is the lower level interface for any stream of bytes, and is what lets you read a specific number of bytes into a preallocated buffer. Seek is implemented for files, but not all streams, because in most cases it makes no sense. There are also a number of file-specific methods directly attached to File for handling permissions and sync and such.

BufRead is the trait that lets you read all the lines, but it is explicitly not implemented for File, since using it for a File would be slow, and generally the wrong way to do things. It does have an implementation for Stdin, because that is already buffered, but if you want to associate a buffer with a file, you have to do it yourself by wrapping it in a ReadBuffer.

If I were to find a problem with the File documentation, it is that it does not mention BufReader.

Re:Rust's 2017 Roadmap by sametmax on Tuesday February 07, 2017 @11:06AM (#13590234)
Make senses. I tried to apply a solution to the wrong problem.
Re:Rust's 2017 Roadmap by int_19h on Tuesday February 07, 2017 @02:50AM (#13587834)
In C# (from which the original example is), these days - and for like 6 years now - what you do is File.ReadLines, and you get a lazy enumerator.
Re:Rust's 2017 Roadmap by rspeer on Monday February 06, 2017 @08:05PM (#13586015)
Iterators exist.