-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
User-friendly input macros. #67
Conversation
Add `scan!`, `scanln!`, etc. macros which mirror `print!`, `println!`, etc. for doing straightforward input from stdin, a file, or other Reader.
See also issue 6220. |
cc @lifthrasiir (since you wrote a (non-procedural!) macro for this.) |
@huonw Actually I have my own experimental syntax extension named read.rs, and an associated draft design document. In my opinion, the most problematic aspect of such syntax extension is how to return the read values if any. For example, if |
I find it very weird to have this pseudo-initialization code before the call to |
@aepsil0n this is pretty much what I think you could probably get away without the initialisation too, e.g.,
I want to avoid returning a value wrapped in an |
Would this work? let (x, y): (f64, str) = scanln!("{} {}"); It's clear from the context, but I don't know how powerful the macro system is. |
This comment IMO ties into questions raised about the More specifically: there is a viewpoint put forward in the comments for that ticket that if you want to handle errors printing to stdout, you should be using
|
@aepsil0n it might. I'm also not so clear on if the macro system is powerful enough to do that. It works for @pnkfelix yeah, I agree with that viewpoint on |
I disagree with several of the assumptions made in this RFC. scan should not be perfectly symmetrical with the way In my experience, there are only two kinds of input:
... and I've found that it makes for better error messages if I implement token-based input on top of line-based input. In my C++ codebase, I currently have the following kinds of splitters:
|
@o11c I think you are right for a general purpose IO library. But I am proposing scan as a 'toy' IO library. The kind if thing which is suited for programming exercises (in the tutorials, or for a university course, for example) or programming competitions. We just want ease of use, really. Robustness, extensibility, and efficiency are not primary as they would be for a real-world IO library. |
@nick29581 I completely disagree that a toy library is a good idea - and that is certainly not something that parallels It's no harder to use my libraries than to use scanf, and it's much safer. |
@o11c I guess we just have to disagree. I can see the merit in your approach though. My feeling is that when teaching, you should teach one thing at a time, so when teaching about IO, you should teach the good stuff that matters, but when teaching something else where IO is peripheral and you just want to get some input to make a fun exercise, then you just want something that is as simple as possible. Any boilerplate at all is a distraction. There are certainly pros and cons for matching scan with print, they are not doing the same thing, but then the symmetry is appealing. I'm not sure from your library description how they are used, but it seems more complex than scanf, which is just a single function call. In the same way that println! exists just to do output in exercies/prototyping/debugging, I think there should be something similarly simple for input. |
Nope. All of the below linked code is replacing calls to
For
(obviously in Rust these would be a trait implementation rather than overloaded functions) Links to how simple or complicated
Also, the human-facing functions which are only called the same file they're defined in:
I haven't linked |
Perhaps:
|
This was discussed in today's meeting and it was decided that a feature such as this should bake in a library before being accepted into the main repo, so I'm going to close this for now. This would be a nice feature to have though! |
I don't know whether you've considered the possibility of matching/parsing either all or nothing (but nothing in between). If the whole thing matches, then all variables are assigned. Otherwise all variables are unset (or nil'd/zero'd) and all the characters read are 'ungot'. This means you can 'try' various patterns against the input 100% safely. This approach worked well for one parsing library I wrote. |
Looks like there's a library that's doing this: https://github.com/mahkoh/scan Also, should this be closed or closed as postponed? |
Fix typo in zip documentation
Add
scan!
,scanln!
, etc. macros which mirrorprint!
,println!
, etc. for doing straightforward input from stdin, a file, or other Reader.I really miss having something like this. I think it would be really good for increasing uptake of Rust to have a good story here.
I was hacking on a prototype implementation over the weekend. It only supports
{}
holes (although some of the infrastructure for fixed widths is there) and doesn't work yet, but its getting there. I'd like to land something like that and then extend the mini-language gradually, letting the design evolve.